Hi,
Thanks for your quick reply.
So, as i understand, it is indeed because i use a proxy, which will be an instance of TrackService ... why Spring thinks i have two beans of type TrackService.
I tried your first option, and it worked. But i would like to make the bean a ref-bean because i might reuse it in other tests. (In production, this is a ref-bean, because it will be used in multiple struts-actions).
I'll try your second option, but my tests weren't called yet. But perhaps it will be when i override the constructor for that test. I tried to set the auto-wire to byName by calling the setter in my test method, but that didn't succeed. I also tried typing the auto-wire="byName" in the applicationContext.xml files in all three beans, but that didn't succeed.
Now, i am using your third option.
I have made a ServiceTestCase class which extends AbstractDependencyInjectionSpringContextTests, with the following code:
Code:
private String[] paths = { "applicationContext*test.xml" };
/**
* @see org.springframework.test.AbstractDependencyInjectionSpringContextTests#getConfigLocations()
*/
protected String[] getConfigLocations() {
return paths;
}
/**
* Sets the path relative to the classpath of the Spring configuration files
* used for unit testing
*
* @param paths
* paths
*/
protected void setApplicationContextPath(String[] paths) {
this.paths = paths;
}
My TrackServiceTest, which extends ServiceTestCase, is implemented as follows:
Code:
private TrackService trackService;
/**
* @see be.dolmen.cdlib.services.ServiceTestCase#onSetUp()
*/
protected void onSetUp() throws Exception {
super.onSetUp();
trackService = (TrackService) applicationContext
.getBean("trackService");
}
/**
* @see be.dolmen.cdlib.services.ServiceTestCase#onTearDown()
*/
protected void onTearDown() throws Exception {
super.onTearDown();
trackService = null;
}
/**
* @throws Exception
* When an Exception occurs in this test.
*/
public void testTrackServiceInjection() throws Exception {
assertNotNull(trackService);
}
I would've liked to use Spring as much as possible, to inject my services in my unit tests, where i would like to test the service (One per unit test).
But, perhaps you're right, and i shouldn't be testing dependency injection in my unit tests but use "trackService = new TrackServiceImpl();" in the onSetup method of my unit test, instead of injecting it.
And just test it that way, instead of letting Spring handle creation and injection of the service into my unit test.
What do you think?
Thank you very much