目前,我使用@Autowired,@Component,@Resource等技术连接了我的弹簧应用程序。现在我想编写一个测试,需要模拟系统中的特定对象,剩余物体的真实生产线。将模拟注入到不带xml的Spring容器中进行测试
让我们假设被测试的根对象被称为Foo。深入依赖链,Foo取决于Bar。目前,模拟出这个对象,我创建了一个测试的applicationContext.xml刚刚的一个bean条目:
<bean id="bar" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.package.Bar" />
</bean>
的测试的applicationContext.xml在@ContextConfiguration注解声明,所以foo将最终取决于模拟的Bar对象。
是否有可能得到相同的结果,而不必拥有这个test-applicationContext.xml?即按相同的步骤进行?
因此,在伪代码,我在想,如果我能有这样的:
public class MyTest {
@Test
public void simple() {
// create mock Bar object
// register mock Bar object in container
// resolve Foo object with all its dependencies but use the mock Bar object instead of the real one
// set some expectations on the Bar object
// call some method on Foo
}
}