Issue
I am have a normal Spring Application (Not a Spring Boot )
When I am running the Integration Test using Spock , the dependent dataUtils Autowired is null
These are my classes
RunWith(SpringRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class)
class DataProcessorTest extends Specification {
def 'call '() {
given:
DataProcesser dataProcessor = new DataProcesser()
when:
dataProcessor.importData()
then:
assert 2 == 2
}
}
Can anybody please let me know what could be the issue ??
Solution
You create DataProcessor
object as normal object using new
, not as bean.
@Autowired
is a method of automatically injecting a bin into an object created as bin.
If you use @Autowired
and dataUtils
in DataProcessor
is not null, you must create DataProcessor
for bean, like below.
@RunWith(SpringRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AppConfig.class)
class DataProcessorTest extends Specification {
@Autowired
DataProcessor dataProcessor;
def 'call '() {
when:
dataProcessor.importData()
then:
assert 2 == 2
}
}
Here is a link for your reference.
https://www.baeldung.com/spring-autowire
Answered By - changuk
Answer Checked By - David Marino (JavaFixing Volunteer)