Announcement Announcement Module
No announcement yet.
JUnit with ContextConfiguration - run custom code before instantiation? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JUnit with ContextConfiguration - run custom code before instantiation?


    My JUnit tests rely on spring injection (using "@ContextConfiguration").
    However, I'd like to run some custom code *before* spring instantiates any beans.
    For example, say I have a Data-Access Object "EmployeeDAO", whose constructor relies on a pre-existing SQL table.
    So I'd like to run my code ( "CREATE TABLE" SQL) just before spring instantiates the DAO.

    I tried the following but it doesn't work, because spring instantiates (and injects) the DAO *before* my "create table" code:
    public class MyTest{
        @Autowired private EmployeeDAO employeeDAO; 
        @Before public void createTable(){
                MySqlUtil.runScript("create table employee...");
                // I'd like this code to run *before* the instantiation of  employeeDAO
        @Test  public void testHire() throws Exception {
    Is there an elegant way to achieve this?
    Of course I could give up '@ContextConfiguration', and explicitly call 'new ClasspathXmlApplicationContext()' whenever I want... but I wondered whether I can still enjoy the elegant injection of '@ContextConfiguration', just run some code before it.


  • #2

    I can help out with a few ideas... maybe someone else can chip in with others.

    1. Use @BeforeClass (and @AfterClass if needed). Same concept as @Before but instead of executing the code before each test it does only once before the test class is instantiated, before Spring application context is loaded, and any test method called.

    2. Do your required logic and then load the application context yourself (do not use @RunWith and @ContextConfiguration) by doing ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/conf/test-conf.xml");. You'll be giving up what Spring is all about...Dependency Injection, and will need to retrieve beans from the context manually among other drawbacks. I wouldn't recommend doing this and I shouldn't even present it as an option but depending on the complexity of your test it might just do the trick.

    3. Use DBUnit. If you need to do things such as stage tables and data on DB before test, rollback anything the test has done to DB, assert state of DB at a particular point on test with expected state, etc. you might want to look at DBUnit. DBUnit will stage the tables for you and is a great tool to assert the state of your database. I like this option best for doing integration tests.

    I hope it helps.



    • #3
      I use jdbc initialize-database in my testing

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns=""
      <jdbc:initialize-database data-source="dataSource">
      		<jdbc:script location="classpath:dbscripts/initdb.sql"/>
      You can define all your ddl/dml sql in the initdb.sql, You can tear down data base in your test or if you want leverage spring use spring aware bean to tear db down