Close

Spring Core Testing - Using @IfProfileValue with JUnit 4

This example demonstrates how to use @IfProfileValue.

@IfProfileValue annotation is used on JUnit tests (doesn't work for others). When used on a method, which is also annotated with @Test, the method will be ignored given that: for a specified property key, the corresponding specified value (or one of the values; we can specify multiple values) does not match with any runtime Environment properties.

By default, it only works for Java system property but we can change that by implementing a custom ProfileValueSource (we will see that in the next tutorial). By default SystemProfileValueSource(an implementation of ProfileValueSource) is used which uses system properties as the underlying source.

When @IfProfileValue is used on the class level, all methods can be ignored based on the same condition.

Deprecation

Since Spring Framework 7.0, @IfProfileValue annotation was deprecated because features provided by this annotation are now natively supported in JUnit 5+ through the @EnabledIf... and @DisabledIf... annotations. Check out these JUnit 5+ tutorials.

Example

Creating a Spring application

@Service
public class MyService {

    public String getMsg() {
        return "some msg";
    }
}
@Configuration
@ComponentScan
public class AppConfig {
}

The JUnit test

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class MyServiceTests {

    @Autowired
    private MyService myService;

    @Test
    @IfProfileValue(name = "os.name", values = {"Windows 10", "Windows 7"})
    public void testMyService() {
        String s = myService.getMsg();
        System.out.println(s);
        Assert.assertEquals("some msg", s);
    }
}
$ mvn test -Dtest=MyServiceTests -DfailIfNoTests=false
[INFO] Scanning for projects...
[INFO]
[INFO] ----------< com.logicbig.example:if-profile-value-annotation >----------
[INFO] Building if-profile-value-annotation 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ if-profile-value-annotation ---
[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\if-profile-value-annotation\src\main\resources
[INFO]
[INFO] --- compiler:3.3:compile (default-compile) @ if-profile-value-annotation ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ if-profile-value-annotation ---
[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\if-profile-value-annotation\src\test\resources
[INFO]
[INFO] --- compiler:3.3:testCompile (default-testCompile) @ if-profile-value-annotation ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ if-profile-value-annotation ---
[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.MyServiceTests
some msg
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.316 s -- in com.logicbig.example.MyServiceTests
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.968 s
[INFO] Finished at: 2026-02-03T08:30:05+08:00
[INFO] ------------------------------------------------------------------------

If run on other O.S or simply property value doesn't match, then the test will be skipped:

$ mvn test -Dtest=MyServiceTests -DfailIfNoTests=false

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyServiceTests
Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.138 sec

Results :

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

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

if-profile-value-annotation Select All Download
  • if-profile-value-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example
      • test
        • java
          • com
            • logicbig
              • example
                • MyServiceTests.java

    See Also

    Join