Reputation: 21
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?
Upvotes: 0
Views: 575
Reputation: 1467
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.
Upvotes: 0