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 is an abstract class.and it is autowired with ApplicationContext which is an interface.
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?
Unit tests should run without applicationContext being built. You have to replace it with a mock object which you will then pass into your object's constructor in the test.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.