Skip to content

TestContext framework should support declarative configuration of bean definition profiles [SPR-7960] #12615

Closed

Description

@spring-projects-issues

Sam Brannen opened SPR-7960 and commented

Overview

Spring 3.1 introduces support for bean definition profiles, such as 'production', 'dev', etc.

The Spring TestContext framework should be extended to support declarative configuration of bean definition profiles on a per-ApplicationContext basis.

One option would be to provide an attribute in the @ContextConfiguration annotation to allow the spring.profiles.active value to be set declaratively.

Further Resources

Example Tests Using @ActiveProfiles

The following examples -- which are taken from the Spring test suite -- show how to declare which active profiles to use when loading an application context via the new @ActiveProfiles annotation.


Testing Active Profiles with @Configuration Classes

\

  • DefaultProfileConfig: @Configuration class without a @Profile declaration
  • DevProfileConfig: @Configuration class with a @Profile("dev") declaration
  • DefaultProfileAnnotationConfigTests: JUnit 4 test that instructs the TestContext framework to load an application context from both the DefaultProfileConfig and DevProfileConfig @Configuration classes
  • DevProfileAnnotationConfigTests: extends DefaultProfileAnnotationConfigTests and instructs the TestContext framework to activate the "dev" profile via @ActiveProfiles("dev")

Note that the autowired employee is null in DefaultProfileAnnotationConfigTests but not null in DevProfileAnnotationConfigTests; whereas, the pet is non-null for both the default and dev profile.

@Configuration
public class DefaultProfileConfig {

	@Bean
	public Pet pet() {
		return new Pet("Fido");
	}
}

\

@Profile("dev")
@Configuration
public class DevProfileConfig {

	@Bean
	public Employee employee() {
		Employee employee = new Employee();
		employee.setName("John Smith");
		employee.setAge(42);
		employee.setCompany("Acme Widgets, Inc.");
		return employee;
	}
}

\

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { DefaultProfileConfig.class, DevProfileConfig.class },
  loader = AnnotationConfigContextLoader.class)
public class DefaultProfileAnnotationConfigTests {

	@Autowired
	protected Pet pet;

	@Autowired(required = false)
	protected Employee employee;

	@Test
	public void pet() {
		assertNotNull(pet);
		assertEquals("Fido", pet.getName());
	}

	@Test
	public void employee() {
		assertNull("employee bean should not be created for the default profile", employee);
	}
}

\

@ActiveProfiles("dev")
public class DevProfileAnnotationConfigTests extends DefaultProfileAnnotationConfigTests {

	@Test
	@Override
	public void employee() {
		assertNotNull("employee bean should be loaded for the 'dev' profile", employee);
		assertEquals("John Smith", employee.getName());
	}

}

Testing Active Profiles with XML Configuration

\
The DefaultProfileXmlConfigTests and DevProfileXmlConfigTests classes are analogous to the DefaultProfileAnnotationConfigTests and DevProfileAnnotationConfigTests classes described above. The difference is that both of these test classes use the XML-based configuration found in DefaultProfileXmlConfigTests-context.xml, which combines the declaration of the default and dev profiles in a single XML file. Otherwise, the use of @ActiveProfiles and the behavior of the tests is identical.

<?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-3.1.xsd">

    <bean id="pet" class="org.springframework.beans.Pet">
        <constructor-arg value="Fido" />
    </bean>

    <beans profile="dev">
        <bean id="employee" class="org.springframework.beans.Employee">
            <property name="name" value="John Smith" />
            <property name="age" value="42" />
            <property name="company" value="Acme Widgets, Inc." />
        </bean>
    </beans>

</beans>

\

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DefaultProfileXmlConfigTests {

	@Autowired
	protected Pet pet;

	@Autowired(required = false)
	protected Employee employee;


	@Test
	public void pet() {
		assertNotNull(pet);
		assertEquals("Fido", pet.getName());
	}

	@Test
	public void employee() {
		assertNull("employee bean should not be created for the default profile", employee);
	}
}

\

@ActiveProfiles("dev")
public class DevProfileXmlConfigTests extends DefaultProfileXmlConfigTests {

	@Test
	@Override
	public void employee() {
		assertNotNull("employee bean should be loaded for the 'dev' profile", employee);
		assertEquals("John Smith", employee.getName());
	}

}

Affects: 3.1 M1

Issue Links:

5 votes, 6 watchers

Activity

  1. spring-projects-issues commented on Mar 10, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    This issue is basically a duplicate of #12410.

  2. spring-projects-issues commented on Apr 6, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    This issue is not really a duplicate of #12410; it's related but not a duplicate.

    This issue aims to make sure that a developer can, as a bare minimum, declaratively set the active profile to use for the application context that is loaded.

    @IfProfileValue has completely different semantics: it determines if the test class or test method should be executed for a given test suite, which potentially has nothing to do with which active bean definition profile to use.

    I'm therefore reopening this issue to make sure that both topics are considered in their own right.

  3. spring-projects-issues commented on Jun 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Introduced @ActiveProfiles annotation for declarative configuration of bean definition profiles in tests.

    See the "Example Tests Using @ActiveProfiles" in the description of this issue for working examples demonstrating the use of @ActiveProfiles in both XML and @Configuration based test classes.

  4. added
    in: test
    type: enhancement
    on Jan 11, 2019
  5. added this to the 3.1 M2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testtype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions