2

我有一些在@PostConstruct方法中查询数据库的 bean。因此,我需要在自动装配发生之前准备好数据库。这可能吗?

这是我的测试课:

@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() {
         ...
    }

}

这是查询内部数据库的 bean @PostConstruct

public class MyBean {
    @Autowire
    private SomeService someService;

    private List someList;

    @PostConstruct
    public void init()  {
         someList = someService.queryDB();
    }
}

这是我要测试的类的一个例子

public class ClassUnderTest {
    @Autowire
    private MyBean bean;


    public void methodToTest() {
        bean.getSomeList();
        // do Something
    }
}
4

0 回答 0