Spring Testing - ProfileValueSource Example

[Last Updated: Jul 3, 2017]

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 last 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.


Creating a Simple Spring application

public class ReportService {

  public String getReport() {
      return "some report";
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("");
      if (resource.exists()) {
          try {
              this.testProps = PropertiesLoaderUtils.loadProperties(resource);
          } catch (IOException e) {
              throw new RuntimeException(e);
          testProps = new Properties();

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



The JUnit test

@ContextConfiguration(classes = AppConfig.class)
public class ServiceTests {

  private ReportService reportService;

  @IfProfileValue(name = "report.enabled", value = "true")
  public void testReport() {
      String s = reportService.getReport();
      Assert.assertEquals("some report", s);
mvn -q test -Dtest=ServiceTests -DfailIfNoTests=false


d:\example-projects\spring-core-testing\profile-value-source-example>mvn -q test -Dtest=ServiceTests -DfailIfNoTests=false

Running com.logicbig.example.ServiceTests
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.136 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 1

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.3.9

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

    See Also