Junit test case to insert data in DB
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
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>
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"); } }
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 } } }
精彩评论