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:
Is there an elegant way to achieve this?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(...); } }
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.


Reply With Quote