spring-framework / org.springframework.test.annotation

Package org.springframework.test.annotation

Types

ProfileValueUtils

abstract class ProfileValueUtils

General utility methods for working with profile values.

SystemProfileValueSource

open class SystemProfileValueSource : ProfileValueSource

Implementation of ProfileValueSource which uses system properties as the underlying source.

TestAnnotationUtils

open class TestAnnotationUtils

Collection of utility methods for working with Spring's core testing annotations.

Annotations

Commit

class Commit

@Commit is a test annotation that is used to indicate that a test-managed transaction should be committed after the test method has completed.

Consult the class-level Javadoc for org.springframework.test.context.transaction.TransactionalTestExecutionListener for an explanation of test-managed transactions.

When declared as a class-level annotation, @Commit defines the default commit semantics for all test methods within the test class hierarchy. When declared as a method-level annotation, @Commit defines commit semantics for the specific test method, potentially overriding class-level default commit or rollback semantics.

Warning: @Commit can be used as direct replacement for @Rollback(false); however, it should not be declared alongside @Rollback. Declaring @Commit and @Rollback on the same test method or on the same test class is unsupported and may lead to unpredictable results.

DirtiesContext

class DirtiesContext

Test annotation which indicates that the org.springframework.context.ApplicationContext associated with a test is dirty and should therefore be closed and removed from the context cache.

Use this annotation if a test has modified the context — for example, by modifying the state of a singleton bean, modifying the state of an embedded database, etc. Subsequent tests that request the same context will be supplied a new context.

@DirtiesContext may be used as a class-level and method-level annotation within the same class or class hierarchy. In such scenarios, the ApplicationContext will be marked as dirty before or after any such annotated method as well as before or after the current test class, depending on the configured #methodMode and #classMode.

As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.

Supported Test Phases
  • Before current test class: when declared at the class level with class mode set to BEFORE_CLASS
  • Before each test method in current test class: when declared at the class level with class mode set to BEFORE_EACH_TEST_METHOD
  • Before current test method: when declared at the method level with method mode set to BEFORE_METHOD
  • After current test method: when declared at the method level with method mode set to AFTER_METHOD
  • After each test method in current test class: when declared at the class level with class mode set to AFTER_EACH_TEST_METHOD
  • After current test class: when declared at the class level with class mode set to AFTER_CLASS

BEFORE_* modes are supported by the org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener; AFTER_* modes are supported by the org.springframework.test.context.support.DirtiesContextTestExecutionListener.

IfProfileValue

class IfProfileValue

Test annotation to indicate whether a test is enabled or disabled for a specific testing profile.

In the context of this annotation, the term profile refers to a Java system property by default; however, the semantics can be changed by implementing a custom ProfileValueSource. If the configured ProfileValueSource returns a matching #value for the declared #name, the test will be enabled. Otherwise, the test will be disabled and effectively ignored.

@IfProfileValue can be applied at the class level, the method level, or both. Class-level usage of @IfProfileValue takes precedence over method-level usage for any methods within that class or its subclasses. Specifically, a test is enabled if it is enabled both at the class level and at the method level; the absence of @IfProfileValue means the test is implicitly enabled. This is analogous to the semantics of JUnit's org.junit.Ignore annotation, except that the presence of @Ignore always disables a test.

Example When using SystemProfileValueSource as the ProfileValueSource implementation (which is configured by default), you can configure a test method to run only on Java VMs from Oracle as follows:
 @IfProfileValue(name = "java.vendor", value = "Oracle Corporation") public void testSomething() { // ... }
'OR' Semantics

You can alternatively configure @IfProfileValue with OR semantics for multiple #values. The following test will be enabled if a ProfileValueSource has been appropriately configured for the "test-groups" profile with a value of either unit-tests or integration-tests. This functionality is similar to TestNG's support for test groups and JUnit's experimental support for test categories.

 @IfProfileValue(name = "test-groups", values = { "unit-tests", "integration-tests" }) public void testWhichRunsForUnitOrIntegrationTestGroups() { // ... }
@IfProfileValue vs. @Profile

Although the @IfProfileValue and org.springframework.context.annotation.Profile annotations both involve profiles, they are not directly related. @Profile involves bean definition profiles configured in the org.springframework.core.env.Environment; whereas, @IfProfileValue is used to enable or disable tests.

Meta-annotation Support

As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.

ProfileValueSourceConfiguration

class ProfileValueSourceConfiguration

ProfileValueSourceConfiguration is a class-level annotation which is used to specify what type of ProfileValueSource to use when retrieving profile values configured via the IfProfileValue annotation.

As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.

Repeat

class Repeat

Test annotation to indicate that a test method should be invoked repeatedly.

Note that the scope of execution to be repeated includes execution of the test method itself as well as any set up or tear down of the test fixture.

As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.

Rollback

class Rollback

@Rollback is a test annotation that is used to indicate whether a test-managed transaction should be rolled back after the test method has completed.

Consult the class-level Javadoc for org.springframework.test.context.transaction.TransactionalTestExecutionListener for an explanation of test-managed transactions.

When declared as a class-level annotation, @Rollback defines the default rollback semantics for all test methods within the test class hierarchy. When declared as a method-level annotation, @Rollback defines rollback semantics for the specific test method, potentially overriding class-level default commit or rollback semantics.

As of Spring Framework 4.2, @Commit can be used as direct replacement for @Rollback(false).

Warning: Declaring @Commit and @Rollback on the same test method or on the same test class is unsupported and may lead to unpredictable results.

This annotation may be used as a meta-annotation to create custom composed annotations. Consult the source code for Commit for a concrete example.

Timed

class Timed

Test-specific annotation to indicate that a test method has to finish execution in a specified time period.

If the text execution takes longer than the specified time period, then the test is considered to have failed.

Note that the time period includes execution of the test method itself, any Repeat of the test, and any set up or tear down of the test fixture.

As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.