Spring @ContextConfiguration Classes Example

0

In this article we will see how to load context using @ContextConfiguration and the classes attribute. The annotated classes to be loaded are specified in the classes attribute.

Load Context using @ContextConfiguration classes attribute

Define few beans using @Configuration annotation.

BeanA:

package com.javarticles.spring;

import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanA {

}

BeanB:

package com.javarticles.spring;

import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanB {

}

Below is a test bean defined in test package.

TestBeanC:

package com.javarticles.spring;

import org.springframework.context.annotation.Configuration;

@Configuration
public class TestBeanC {

}

We will now load all three annotated classes using @ContextConfiguration.

SpringContextConfigurationClassesExample:

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(classes={BeanA.class, BeanB.class, TestBeanC.class})
public class SpringContextConfigurationClassesExample {
    @Autowired
    private BeanA beanA;
    
    @Autowired
    private BeanB beanB;
    
    @Autowired
    private TestBeanC beanC;
    
    @Test
    public void verifyBeans() {
        assertNotNull(beanA);
        assertNotNull(beanB);
        assertNotNull(beanC);
    }
}

If you try to use both locations and classes attributes then spring will throw error, only one of them needs to be specified.

SpringContextConfigurationLocationAnsClassesExample:

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(locations = "/context1.xml", classes = { BeanA.class,
        BeanB.class})
public class SpringContextConfigurationLocationAnsClassesExample {
    @Autowired
    private BeanA beanA;

    @Autowired
    private BeanB beanB;

    @Autowired
    private TestBeanC beanC;

    @Test
    public void verifyBeans() {
        assertNotNull(beanA);
        assertNotNull(beanB);
        assertNotNull(beanC);
    }
}

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="beanC" class="com.javarticles.spring.TestBeanC" />

</beans>

Output:

java.lang.IllegalArgumentException: Cannot process locations AND classes for context configuration 

Default Context Loading without classes attribute

If you don’t specify the classes attribute,the test class will automatically load any static inner components defined using spring annotations.

SpringContextConfigurationDefaultClassesExample:

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.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringContextConfigurationDefaultClassesExample {
    @Autowired
    private TestA beanA;
    
    @Test
    public void verifyBeans() {
        assertNotNull(beanA);
    }
    
    @Configuration
    static class TestA {
        
    }
}

Download the source code

This was an example about loading context using spring @ContextConfiguration and classes attribute.

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

Comments are closed.