The following annotations are only supported when used in combination with SpringRunner, rules for JUnit 4 in Spring or JUnit 4 helper classes in Spring:

@IfProfileValue

The @IfProfileValue annotation indicates that the annotated test is enabled for a specific test environment. If the configured ProfileValueSource returns a matching value for the specified name, then the test is activated. Otherwise, the test is disabled and effectively ignored.

You can apply the @IfProfileValue annotation at the class level, at the method level, or at both levels. Use of the @IfProfileValue annotation at the class level takes precedence over use at the method level for any methods within that class or its subclasses. Specifically, a test is considered activated if it is activated at both the class and method levels. The absence of the @IfProfileValue annotation means that the test is implicitly activated. This matches the semantics of the @Ignore annotation from JUnit 4, except that the presence of the @Ignore annotation always disables the test.

The following example shows a test with the @IfProfileValue annotation:

Java

@IfProfileValue(name="java.vendor", value="Oracle Corporation") 
@Test
public void testProcessWhichRunsOnlyOnOracleJvm() {
    // some logic that should only run on Oracle Corporation's Java VM
}
  1. We only perform this test if the Java vendor is Oracle Corporation.
Kotlin

@IfProfileValue(name="java.vendor", value="Oracle Corporation") 
@Test
fun testProcessWhichRunsOnlyOnOracleJvm() {
    // some logic that should only run on Oracle Corporation's Java VM
}
  1. We only perform this test if the Java vendor is Oracle Corporation.

Alternatively, you can configure the @IfProfileValue annotation using a list of values (with OR semantics) to obtain TestNG-like support for groups of tests in JUnit 4 environment. Consider the following example:

Java

IfProfileValue(name="test-groups", values={"unit-tests", "integration-tests"}) 
@Test
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
    // some logic that should only be executed for groups of unit and integration tests
}
  1. We perform this test for unit and integration tests.
Kotlin

@IfProfileValue(name="test-groups", values=["unit-tests", "integration-tests"]) 
@Test
fun testProcessWhichRunsForUnitOrIntegrationTestGroups() {
    // some logic that should only be executed for groups of unit and integration tests
}
  1. We perform this test for unit and integration tests.

@ProfileValueSourceConfiguration

@ProfileValueSourceConfiguration is a class-level annotation that specifies what type of ProfileValueSource to use when retrieving profile values configured with the @IfProfileValue annotation. If the @ProfileValueSourceConfiguration annotation is not declared for a test, then the default is SystemProfileValueSource. The following example shows how to use the @ProfileValueSourceConfiguration annotation:

Java

@ProfileValueSourceConfiguration(CustomProfileValueSource.class) 
public class CustomProfileValueSourceTests {
    // class body...
}
  1. Use a special profile value source.
Kotlin

@ProfileValueSourceConfiguration(CustomProfileValueSource::class) 
class CustomProfileValueSourceTests {
    // class body...
}
  1. Use a special profile value source.

@Timed

The @Timed annotation specifies that the annotated test method must complete execution within a specified period of time (in milliseconds). If the text execution time exceeds the specified period of time, the test fails.

The time period includes execution of the test method itself, any repetitions of the test (see @Repeat), and setup and disassembly of the test bench. The following example shows how to use it:

Java

@Timed(millis = 1000) 
public void testProcessWithOneSecondTimeout() {
    // some logic that shouldn't take more than 1 second to complete
}
  1. Set the time period for the test to one second.
Kotlin

@Timed(millis = 1000) 
fun testProcessWithOneSecondTimeout() {
    // some logic that shouldn't take more than 1 second to complete
}
  1. Set the time period for the test to one second.

The @Timed annotation from Spring has different semantics than that provided by the @Test(timeout=…) annotation facilities from JUnit 4. In particular, from -the way JUnit 4 handles test execution timeouts (i.e., executing the test method in a separate Thread), @Test(timeout=...) in proactive mode, the test fails if the test takes too long. On the other hand, the @Timed annotation in Spring does not fail the test proactively, but waits for the test to complete before it fails.

@Repeat

The @Repeat annotation specifies that the annotated test method should be executed multiple times. The number of runs of the test method is indicated in the annotation.

The scope of re-execution includes the execution of the test method itself, as well as the setup and disassembly of the test bench. When used with SpringMethodRule the scope additionally includes preparation test instance implementation TestExecutionListener. The following example shows how to use the @Repeat annotation:

Java

@Repeat(10) 
@Test
public void testProcessRepeatedly() {
    // ...
}
  1. Repeat this test ten times.
Kotlin

@Repeat(10) 
@Test
fun testProcessRepeatedly() {
    // ...
}
  1. Repeat this test ten times.