Spring @TestPropertySource Annotation Example

0

In this article we will see how to use spring annotation @TestPropertySource to override the existing properties defined in the system and application property sources. @TestPropertySource is a class-level annotation that is similar to @PropertySource where the properties files can be loaded using the locations attribute which is also the default attribute.

Spring @PropertySource vs @TestPropertySource

Test property sources have higher precedence than those loaded from the operating system’s environment or Java system properties as well as property sources added using @PropertySource.

In the below test class, we load Config class which in turn loads prod.properties.

Config:

package com.javarticles.spring;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:/com/javarticles/spring/prod.properties")
public class Config {
}

prod.properties:

name=Spring @TestPropertySource Annotation Example

The spring Environment auto-wired and test verifyProperty() fetches property name which as expected prints the value specified in prod.properties as ‘Spring @TestPropertySource Annotation Example’.

SpringPropertySourceAnnotationTest:

package com.javarticles.spring;

import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringPropertySourceAnnotationTest.class,
        Config.class })
public class SpringPropertySourceAnnotationTest {
    @Autowired
    protected Environment env;

    @Test
    public void verifyProperty() {
        System.out.println(env.getProperty("name"));
        assertEquals("Spring @TestPropertySource Annotation Example",
                env.getProperty("name"));
    }

}

Output:

Spring @TestPropertySource Annotation Example

test.properties:

name=Overridden by test

Override @PropertySource using @TestPropertySource

We will now override the name property by loading test.properties using @TestPropertySource.

SpringTestPropertySourceAnnotationTest:

package com.javarticles.spring;

import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.test.context.TestPropertySource;

@TestPropertySource("test.properties")
public class SpringTestPropertySourceAnnotationTest extends SpringPropertySourceAnnotationTest {

    @Test
    public void verifyProperty() {
        System.out.println(env.getProperty("name"));
        assertEquals("Overridden by test", env.getProperty("name"));
    }

}

As you can see from the below output, name property is overridden. It now prints ‘Overridden by test’.

Output:

Overridden by test

Download the source code

This was an example about Spring @TestPropertySource annotation.

You can download the source code here: SpringTestPropertySourceAnnotationExample.zip

About Author

Ram’s expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring.
You can reach him at [email protected]

Comments are closed.