2011-10-05 294 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 
        } 
        } 
        }