I have some beans that query the database in a @PostConstruct
method. Thus, I need the database to be ready before autowiring occurs. Is this possible?
This is my test class:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring/app.xml",
"classpath:spring/test-beans-context.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseSetup(value = { "/datasets/dataset1.xml", "/datasets/dataset2.xml" })
public class myTestIT {
@Autowired
private ClassUnderTest classUnderTest;
@Test
public void test() {
...
}
}
This is the bean that queries the DB inside @PostConstruct
:
public class MyBean {
@Autowire
private SomeService someService;
private List someList;
@PostConstruct
public void init() {
someList = someService.queryDB();
}
}
This is an example of the class I want to test
public class ClassUnderTest {
@Autowire
private MyBean bean;
public void methodToTest() {
bean.getSomeList();
// do Something
}
}