开发者

Junit test case to insert data in DB

开发者 https://www.devze.com 2023-04-11 09:45 出处:网络
In 开发者_JS百科my project, I have to implement a JUnit testcase which will insert data in a DB table at the time of server start up.

In 开发者_JS百科my project, I have to implement a JUnit testcase which will insert data in a DB table at the time of server start up. Can anybody suggest how to implement it?


I have listed few solutions here

  1. Using spring init data base, add this to your spring context file, spring will execute the sql script as it boots up container

    <!--  Script with sql to insert statements -->
     <jdbc:initialize-database>
          <jdbc:script location="classpath:initDB.sql"/>
      </jdbc:initialize-database>
    
  2. You can do that using code , This will tie your code to spring interfaces

     @Service
     public class InitializeDBService implements InitializingBean 
     {
    
       @Autowired
        JdbcTemplate jdbcTemplate;
    
       @Override
        public void afterPropertiesSet() throws Exception {
         jdbcTemplate.execute("your inserts");
        }
     }
    
  3. You could use a servlet to do this

     @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
      }
       }  
      }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消