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

  • JUnit with ContextConfiguration - run custom code before instantiation?

    Hi,

    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:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    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 {
             employeeDAO.doSomething();
             assert(...);
        }
    }
    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.

    Thanks.

  • #2
    @solmyr72

    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.


    nicolas.loriente

    Comment


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

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
      		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
      
      <jdbc:initialize-database data-source="dataSource">
      		<jdbc:script location="classpath:dbscripts/initdb.sql"/>
      	</jdbc:initialize-database>
      </beans>
      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

      Comment

      Working...
      X