2011-10-05 83 views
0

在我的項目中,我必須實現一個JUnit測試用例,它將在服務器啓動時將數據插入數據庫表中。 有人可以建議如何實施它?Junit測試用例在數據庫中插入數據

+0

哪個服務器,你用怎麼辦? –

+0

@Matthew Farwell:tomcat5 – Romi

+0

你正在運行什麼應用程序?普通的基於servlet的應用程序或一些mvc框架工作? –

回答

0

我列舉幾個在這裏

  1. 解決方案中使用Spring初始化數據的基礎上,添加到您的Spring上下文文件,春天會因爲它啓動時容器

    <!-- Script with sql to insert statements --> 
    <jdbc:initialize-database> 
         <jdbc:script location="classpath:initDB.sql"/> 
        </jdbc:initialize-database> 
    
  2. 執行SQL腳本你可以使用代碼來做到這一點,這會將你的代碼綁定到彈簧接口上

    @Service 
    public class InitializeDBService implements InitializingBean 
    { 
    
        @Autowired 
        JdbcTemplate jdbcTemplate; 
    
        @Override 
        public void afterPropertiesSet() throws Exception { 
        jdbcTemplate.execute("your inserts"); 
        } 
    } 
    
  3. 你可以使用一個Servlet這

    @SuppressWarnings("serial") 
    public class InitDBServlet extends HttpServlet 
    { 
        public void init() throws ServletException 
        { 
         //spawning the thread so that not to delay servlet start up 
         new Thread(new InitDB()).start(); 
        } 
    
        class InitDB implements Runnable 
        { 
        @Override 
        public void run() 
        { 
        //add your insert code here 
        } 
        } 
        } 
    
相關問題