Spring @ContextConfiguration Annotation Example

0

Spring based integration tests need a way to load and configure application context. We that using spring class-level annotation.
You can either feed in the path-based XML configuration files or annotated class-based resources. In this article, we will look use XML files to load the context.

Loading Context using @ContextConfigfuration

The value attribute is used is an alias for locations. By default, if you don’t provide the location or value attribute then the XML file it will look out for loading is {class}-context.xml.
If the resource is not found then spring will throw error:

java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to detect defaults, and no ApplicationContextInitializers were declared for context configuration

Let’s load a bean using an empty @ContextConfigfuration.

BeanA:

package com.javarticles.spring;

public class BeanA {

}

Below test class uses the empty @ContextConfigfuration. We need to run the test class with SpringJUnit4ClassRunner.

SpringContextConfigurationExample:

package com.javarticles.spring;

import static org.junit.Assert.assertNotNull;

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
public class SpringContextConfigurationExample {
    @Autowired
    private BeanA beanA;
    
    @Test
    public void beanAExists() {
        assertNotNull(beanA);
    }
}

XML file should be named as SpringContextConfigurationExample-context.xml. It should be in the same folder structure as the test class being run.
We auto inject the bean using @Autowired and in the test case beanAExists()), we make sure bean is loaded.

com/javarticles/spring/springContextConfigurationDefaultExample-context.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="beanA" class="com.javarticles.spring.BeanA" />

</beans>

Spring @ContextConfiguration Value

If you specify the XML path within @ContextConfiguration, it will be taken as the locations. The value attribute is an alias for locations.

context1.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="beanA" class="com.javarticles.spring.BeanA" />

</beans>

SpringContextConfigurationValueExample:

package com.javarticles.spring;

import static org.junit.Assert.assertNotNull;

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("/context1.xml")
public class SpringContextConfigurationValueExample {
    @Autowired
    private BeanA beanA;
    
    @Test
    public void beanAExists() {
        assertNotNull(beanA);
    }
}

Loading Context using @ContextConfigutration Locations

In the example, we will load context from multiple locations using locations. We will define couple of more beans.

BeanB:

package com.javarticles.spring;

public class BeanB {

}

BeanC:

package com.javarticles.spring;

public class BeanC {

}

com/javarticles/spring/context2.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="beanB" class="com.javarticles.spring.BeanB" />

</beans>

com/javarticles/spring/context3.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="beanC" class="com.javarticles.spring.BeanC" />

</beans>

SpringContextConfigurationLocationsExample:

package com.javarticles.spring;

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/context1.xml",
        "/com/javarticles/spring/context2.xml",
        "/com/javarticles/spring/context3.xml"})
public class SpringContextConfigurationLocationsExample {
    @Autowired
    private BeanA beanA;

    @Autowired
    private BeanB beanB;
    
    @Autowired
    private ApplicationContext context;
    
    @Test
    public void beanAExists() {
        assertNotNull(beanA);
    }

    @Test
    public void beanBExists() {
        assertNotNull(beanB);
    }
    
    @Test
    public void beanCExists() {
        assertNotNull(context.getBean("beanC"));
    }
}

Merge ContextConfigurations

If the super class also uses @ContextConfiguration then the final context will be based on both the test class and the super classes.

SpringBaseContext:

package com.javarticles.spring;

import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations = "/context1.xml")
public class SpringBaseContext {
}

SpringMergeConfigurationContextExample:

package com.javarticles.spring;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { 
        "/com/javarticles/spring/context2.xml",
        "/com/javarticles/spring/context3.xml" })
public class SpringMergeConfigurationContextExample extends SpringBaseContext {
    @Autowired
    private BeanA beanA;

    @Autowired
    private BeanB beanB;

    @Autowired
    private ApplicationContext context;

    @Test
    public void beanAExists() {
        assertNotNull(beanA);
    }

    @Test
    public void beanBExists() {
        assertNotNull(beanB);
    }

    @Test
    public void beanCExists() {
        assertNotNull(context.getBean("beanC"));
    }

    @Test
    public void hasParent() {
        System.out.println(context.getParent());
    }
}

Download the source code

This was an example about spring @ContextConfiguration annotation.

You can download the source code here: springContextConfigurationAnnotationExample.zip
Share.

Comments are closed.