[英]Groovy Spock How to Wire or Mock Spring Autowired Interface
I have a class like this 我有这样的课
public abstract class JobProcessor {
@Autowired
ApplicationContext applicationContext;
protected void startProcess() {
MyThread myThread= (MyThread) applicationContext.getBean("myThread");
myThread.setConversionObject(new MyObject());
...
}
}
I want to write unit test for JobProcessor. 我想为JobProcessor编写单元测试。 JobProcessor is an abstract class.and it is autowired with ApplicationContext which is an interface.
JobProcessor是一个抽象类。它与作为接口的ApplicationContext自动关联。
My test is like this 我的测试是这样的
@SpringBootTest(classes = JobProcessorApplication.class)
@ContextConfiguration(locations = "classpath:InjectionContext.xml")
@TestPropertySource(locations = "classpath:test.properties")
@Import(UnitTestConfiguration)
class JobProcessorSpec extends Specification {
class JobProcessorChild extends JobProcessor {
@Override
boolean processRequest() {
return false
}
def "Should start process"() {
given:
def jobProcessorChild = new JobProcessorChild()
when:
jobProcessorChild.startProcess()
then:
noExceptionThrown()
}
}
}
My test always fails for nullpoint error of applicationContext Can anyone please guide me how to write unit test properly here? 由于applicationContext的nullpoint错误,我的测试总是失败。有人可以指导我如何在此处正确编写单元测试吗?
Unit tests should run without applicationContext being built. 单元测试应在不构建applicationContext的情况下运行。 You have to replace it with a mock object which you will then pass into your object's constructor in the test.
您必须将其替换为模拟对象,然后将其传递给测试中的对象的构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.