Spring @ContextConfiguration

I run the following test:

import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" }) public class FloorServiceTest { @Autowired private FloorService floorService; @Test public void testFloorService() { floorService.findById((long)1); } } 

With applicationContext.xml in /APP/src/main/resources/META-INF/spring/

But Autowire bean seems to be wrong:

  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.confloorapp.services.FloorServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.confloorapp.services.FloorService com.confloorapp.services.FloorServiceTest.floorService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.confloorapp.services.FloorService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
+6
source share
3 answers

Try

 @ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" }) 

Honestly, I would step back from xml and go this route. Edit

 @ContextConfiguration(locations = { "/META-INF/spring/applicationContext.xml" }) 

to

 @ContextConfiguration(classes = { FloorServiceTestConfig.class }) 

And create a class class

 @Configuration public class FloorServiceTestConfig { @Bean public FloorService floorService() { return new FloorService(); } } 

Thus, when you need to make fun of your beans for a class that you are not testing, it looks below

 @Configuration public class FloorServiceTestConfig { @Bean public FloorService floorService() { return Mockito.mock(FloorService.class); } } 
+11
source

The @Configuration idea above works well. But for this you need to annotate your classes with @Configuration . This is not a good idea when it comes to offline testing. If your organization does not run test cases while you work. In this case, @ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" }) would be a good idea.

To use this method, take care of the following:

  • Check your classpath in the .classpath file of your project.

  • If you cannot write the applicationContext path relative to the class path.

You can have another file in the applicationContextTest folder stored in your test case folder, and then you can use @ContextConfiguration(locations = { "classpath:applicationContextTest.xml"})

+1
source

In your case, you should use:

 @ContextConfiguration(locations = "file:src/main/resources/META-INF/spring/applicationContext.xml") 

Another hint, I see that you are using your production applicationContext.xml in your test environment, IMHO, this is not a good practice, try using a specific one for testing, for example applicationContext-test.xml so that you can play with your test context without damage to your work environment.

0
source

All Articles