Close

Spring Core Testing - ProfileValueSource Example

This example demonstrates how to use ProfileValueSource and @ProfileValueSourceConfiguration to specify a custom strategy for retrieving profile values for a given testing environment.

Rather than using the default SystemProfileValueSource (check out the this tutorial), a custom implementation of ProfileValueSource can be configured on a test class by using @ProfileValueSourceConfiguration. When @IfProfileValue is used in the same class, it's value is tested against the value returned from ProfileValueSource#get(String key) to enable or disable the corresponding test. Let's understand that with an example.

Deprecation

ProfileValueSource has been deprecated in Spring 7 as it was used for legacy JUnit 4 testing.

Recommended Replacements:

  • JUnit 5 Conditions: Use native JUnit Jupiter annotations like @EnabledIfSystemProperty or @EnabledIfEnvironmentVariable
  • SpringExtension: Use SpringExtension for JUnit Jupiter for modern Spring test integration
  • Environment Abstraction: Use Spring's Environment or PropertySource for property retrieval

Note:

Related legacy utilities (ProfileValueUtils and @IfProfileValue) are also deprecated without direct replacements, as Spring now favors JUnit 5's related native features.

Check out these JUnit 5+ tutorials.

Also Check out these Spring Core Testing with JUnit 5+ tutorials

Example

Creating a Simple Spring application

@Service
public class ReportService {

    public String getReport() {
        return "some report";
    }
}
@Configuration
@ComponentScan
public class AppConfig {

}

Implementing ProfileValueSource

This implementation first attempts to retrieve the value for the provided 'key' from Properties loaded from a property file. If not present (if null) then tries the System properties for the same key.

public class MyProfileValueSource implements ProfileValueSource {
    private final Properties testProps;

    public MyProfileValueSource() {

        ClassPathResource resource = new ClassPathResource("test.properties");
        if (resource.exists()) {
            try {
                this.testProps = PropertiesLoaderUtils.loadProperties(resource);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }else{
            testProps = new Properties();
        }
    }

    @Override
    public String get(String key) {
        return testProps.getProperty(key, System.getProperty(key));
    }
}

src/main/resources/test.properties

report.enabled=false

The JUnit test

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@ProfileValueSourceConfiguration(MyProfileValueSource.class)
public class ServiceTests {

    @Autowired
    private ReportService reportService;

    @Test
    @IfProfileValue(name = "report.enabled", value = "true")
    public void testReport() {
        String s = reportService.getReport();
        System.out.println(s);
        Assert.assertEquals("some report", s);
    }
}
$ mvn test -Dtest=ServiceTests -DfailIfNoTests=false
[INFO] Scanning for projects...
[INFO]
[INFO] ---------< com.logicbig.example:profile-value-source-example >----------
[INFO] Building profile-value-source-example 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ profile-value-source-example ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource from src\main\resources to target\classes
[INFO]
[INFO] --- compiler:3.3:compile (default-compile) @ profile-value-source-example ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to D:\example-projects\spring-core-testing\spring-core-testing-with-junit-4\profile-value-source-example\target\classes
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ profile-value-source-example ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\example-projects\spring-core-testing\spring-core-testing-with-junit-4\profile-value-source-example\src\test\resources
[INFO]
[INFO] --- compiler:3.3:testCompile (default-testCompile) @ profile-value-source-example ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to D:\example-projects\spring-core-testing\spring-core-testing-with-junit-4\profile-value-source-example\target\test-classes
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ profile-value-source-example ---
[INFO] Using auto detected provider org.apache.maven.surefire.junit4.JUnit4Provider
[WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.logicbig.example.ServiceTests
[WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.101 s -- in com.logicbig.example.ServiceTests
[INFO]
[INFO] Results:
[INFO]
[WARNING] Tests run: 1, Failures: 0, Errors: 0, Skipped: 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.083 s
[INFO] Finished at: 2026-02-03T14:05:08+08:00
[INFO] ------------------------------------------------------------------------

The test was skipped. We can enable the method by setting report.enabled=true in the property file without changing the code.

Example Project

Dependencies and Technologies Used:

  • spring-context 4.3.8.RELEASE (Spring Context)
  • spring-test 4.3.9.RELEASE (Spring TestContext Framework)
  • junit 4.12 (JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck)
  • JDK 1.8
  • Maven 3.9.11

profile-value-source-example Select All Download
  • profile-value-source-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
      • test
        • java
          • com
            • logicbig
              • example

See Also

Join