我有一堂这样的课
public abstract class JobProcessor {
@Autowired
ApplicationContext applicationContext;
protected void startProcess() {
MyThread myThread= (MyThread) applicationContext.getBean("myThread");
myThread.setConversionObject(new MyObject());
...
}
}
我想为JobProcessor编写单元测试。 JobProcessor是一个抽象类,它与作为接口的ApplicationContext自动关联。
我的测试就是这样
@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()
}
}
}
我的测试始终因applicationContext的空点错误而失败 谁能指导我在这里如何正确编写单元测试?
答案 0 :(得分:0)
单元测试应在不构建applicationContext的情况下运行。您必须将其替换为模拟对象,然后在测试中将其传递到对象的构造函数中。