Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @ContextConfiguration

I am running the next 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 the file applicationContext.xml under the folder /APP/src/main/resources/META-INF/spring/

But it seems not to Autowire correctly the bean:

  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)}
like image 525
darkZone Avatar asked May 12 '14 20:05

darkZone


People also ask

What is @contextconfiguration in Spring Boot?

@ContextConfiguration loads an ApplicationContext for Spring integration test. @ContextConfiguration can load ApplicationContext using XML resource or the JavaConfig annotated with @Configuration. The @ContextConfiguration annotation can also load a component annotated with @Component, @Service, @Repository etc.

How do I annotate a test class with @contextconfiguration in spring?

We also annotate our test class with Spring’s @ContextConfiguration annotation without specifying any attributes. In this case the default GenericXmlContextLoader will be used, and following the principle of convention over configuration Spring will load our ApplicationContext from classpath:/com/example/OrderServiceTest-context.xml.

How to load applicationcontext using @contextconfiguration?

@ContextConfiguration can load ApplicationContext using XML resource or the JavaConfig annotated with @Configuration. The @ContextConfiguration annotation can also load a component annotated with @Component, @Service, @Repository etc.

How do I access applicationcontext in Spring Boot?

By default the ApplicationContext is loaded using the GenericXmlContextLoader which loads a context from XML Spring configuration files. You can then access beans from the ApplicationContext by annotating fields in your test class with @Autowired, @Resource, or @Inject.


2 Answers

Try

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

Honestly I would step away from the xml and go this route. Change

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

to

@ContextConfiguration(classes = { FloorServiceTestConfig.class })

And create the about class

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

This way when you need to mock your beans for class you're not testing it looks like below

@Configuration
public class FloorServiceTestConfig
{
    @Bean
    public FloorService floorService()
    {
          return Mockito.mock(FloorService.class);
    }
}
like image 74
ndrone Avatar answered Oct 06 '22 23:10

ndrone


The idea of @Configuration given above works nice. But for that you have to annotate your classes with @Configuration. This is not a nice idea when it comes to testing offline i.e when in your organisation test cases are not executed at built time. In that case going by @ContextConfiguration(locations = { "classpath:/META-INF/spring/applicationContext.xml" }) will be a nice idea.

For using this method, take care of the following:

  1. Check your classpath in .classpath file of your project.

  2. If you cannot write the path of applicationContext relative to classpath.

You can have one more file as applicationContextTest folder kept in your test case folder and then one can use @ContextConfiguration(locations = { "classpath:applicationContextTest.xml"})

like image 28
Shweta Gulati Avatar answered Oct 06 '22 23:10

Shweta Gulati