Spring TestContext Framework Extension for JUnit Jupiter (a.k.a., JUnit 5)
This project served as the official prototype for JUnit 5 testing support in the Spring TestContext Framework and has been incorporated into Spring Framework 5.0 in conjunction with SPR-13575. Consequently, no further work is planned in this repository in terms of new features: new features are only supported in Spring Framework 5.0+. Note, however, that this project can in fact be used for JUnit Jupiter testing support in conjunction with Spring Framework 4.3.x.
SpringExtension
Currently, all that's needed to use the Spring TestContext Framework with JUnit 5 is to annotate a JUnit Jupiter based test class with
@ExtendWith(SpringExtension.class)and whatever Spring annotations you need (e.g.,
@ContextConfiguration,
@Transactional,
@Sql, etc.), but make sure you use
@Test,
@BeforeEach, etc. from the appropriate
org.junit.jupiter.apipackage. See
SpringExtensionTestsfor an example of this extension in action, and check out the source code of
SpringExtensionif you're interested in the implementation details.
Spring has supported composed annotations for several years now, and as of JUnit 5 annotations in JUnit can also be used as meta-annotations. We can therefore create custom annotations that are composed from Spring annotations and JUnit 5 annotations. Take a look at
@SpringJUnitJupiterConfigfor an example, and check out
ComposedSpringExtensionTestsfor an example of
@SpringJUnitJupiterConfigin action.
This project is released under version 2.0 of the Apache License.
There are currently no downloadable artifacts for this project; however, you may opt to install
spring-test-junit5in your local Maven repository or include a dependency on
spring-test-junit5via JitPack. See the following sections for further details.
If you install in a local Maven repository (see below) the generated artifact will correspond to the following.
org.springframework.test
spring-test-junit5
1.0.0.BUILD-SNAPSHOT
If you'd like to build against a release tag for
spring-test-junit5, you may be interested in using JitPack. For example, to build against the
1.5.0tag, the following Maven coordinates will work.
com.github.sbrannen
spring-test-junit5
1.5.0
repositories { mavenCentral() maven { url 'https://jitpack.io' } }// ...
dependencies { // ... testCompile('com.github.sbrannen:spring-test-junit5:1.5.0') // ... }
jitpack.io https://jitpack.io com.github.sbrannen spring-test-junit5 1.5.0 test<!-- ... -->
This project uses a Gradle-based build system. In the instructions below,
./gradlewis invoked from the root of the project and serves as a cross-platform, self-contained bootstrap mechanism for the build.
5.5.0and JUnit Platform
1.5.0
4.3.24.RELEASE
Be sure that your
JAVA_HOMEenvironment variable points to the
jdk1.8.0folder extracted from the JDK download.
Compile code, run tests, and build JARs, distribution ZIP files, and docs:
./gradlew build
spring-test-junit5in local Maven repository
./gradlew publishToMavenLocal
./gradlew test
spring-test-junit5can also be built with and tested against JDK 9.0.4, JDK 10.0.2, and JDK 11.0.2.
In order to execute all of the tests within an IDE as a single suite, simply run
SpringExtensionTestSuiteas a JUnit 4 test class.