I want to create test data at runtime for my junit test cases. As part of that activity i have choose to write a set of data fixtures and an implementation around it which would create/remove/update test data. These would be called from every Junit class @before and @After methods – @before to setup the test data and @after to teardown the test data.
I have written all this implementation inside the “test” package and have marked the main classes as @component and @autowired these new classes. However when i run my junit test it is unable to create the instances of these new classes and hence the autowire does not seem to work.
I am not sure what more i have to do prior to autowire other than adding following configuration inside the test-config.xml
<context:component-scan base-package="test">
</context:component-scan>
I am using Spring 3.x and Junit 4.
———More information————–
//Business implementation:
//File location: src/main/java/com/abc/prq
package com.abc.pqr
@Service("myservice")
public class MyService{
}
//Junit Test for Myservice class:
//File Location: src/test/java/com/abc/pqr
package com.abc.pqr;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:/META-INF/spring/test-Config.xml"})
public class MyServiceTest {
@Autowired
private ABC abc;
}
//Data Fixture to create/remove/update test data
//File Location: src/test/java/com/abc/pqr/datafixtures
package com.abc.pqr.datafixtures
@Component("abc")
public class ABC{
public void create(){
}
public void remove(){
}
public void update(){
}
}
I think that the problem is caused by the fact that you’re not scanning the right package in the spring test context.
The
test-Config.xmlfile should look something like this:Hope this helps.