Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.wicksome:spring-test-junit5:1.0.0.M6'
}
dependencies {
implementation("com.github.wicksome:spring-test-junit5:1.0.0.M6")
}
<dependency>
<groupId>com.github.wicksome</groupId>
<artifactId>spring-test-junit5</artifactId>
<version>1.0.0.M6</version>
</dependency>
libraryDependencies += "com.github.wicksome" % "spring-test-junit5" % "1.0.0.M6"
:dependencies [[com.github.wicksome/spring-test-junit5 "1.0.0.M6"]]
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 as a playground for basic JUnit 5 support in conjunction with Spring Framework 4.3.
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.api
package. See SpringExtensionTests
for an example of this
extension in action, and check out the source code of SpringExtension
if 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 @SpringJUnitJupiterConfig
for an example, and check out
ComposedSpringExtensionTests
for an example of @SpringJUnitJupiterConfig
in 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-junit5
in your local Maven repository
or include a dependency on spring-test-junit5
via 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.0.0.M6
tag, the following Maven coordinates will work.
com.github.sbrannen
spring-test-junit5
1.0.0.M6
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
// ...
dependencies {
// ...
testCompile('com.github.sbrannen:spring-test-junit5:1.0.0.M6')
// ...
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<!-- ... -->
<dependencies>
<dependency>
<groupId>com.github.sbrannen</groupId>
<artifactId>spring-test-junit5</artifactId>
<version>1.0.0.M6</version>
<scope>test</scope>
</dependency>
<!-- ... -->
</dependencies>
This project uses a Gradle-based build system. In the instructions
below, ./gradlew
is invoked from the root of the project and serves as
a cross-platform, self-contained bootstrap mechanism for the build.
5.0.0-M6
and JUnit Platform 1.0.0-M6
4.3.10.RELEASE
Be sure that your JAVA_HOME
environment variable points to the jdk1.8.0
folder
extracted from the JDK download.
Build all JARs, distribution ZIP files, and docs:
./gradlew build
spring-test-junit5
in local Maven repository./gradlew install
Executing gradlew clean test
from the command line should result in output similar to the following.
:junitPlatformTest
Test run finished after 2542 ms
[ 12 containers found ]
[ 0 containers skipped ]
[ 12 containers started ]
[ 0 containers aborted ]
[ 12 containers successful ]
[ 0 containers failed ]
[ 43 tests found ]
[ 0 tests skipped ]
[ 43 tests started ]
[ 0 tests aborted ]
[ 43 tests successful ]
[ 0 tests failed ]
spring-test-junit5
can be built with and tested against
JDK 9 early access builds using
Gradle 4.0.
Assuming we have OpenJDK jdk-9+176
installed, executing
gradlew -version && gradlew clean test
will result in
output similar to the following.
------------------------------------------------------------
Gradle 4.0
------------------------------------------------------------
Build time: 2017-06-14 15:11:08 UTC
Revision: 316546a5fcb4e2dfe1d6aa0b73a4e09e8cecb5a5
Groovy: 2.4.11
Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM: 9 (Oracle Corporation 9+176)
OS: Mac OS X 10.12.5 x86_64
:junitPlatformTest
╷
└─ JUnit Jupiter ✔
├─ @SpringJUnitJupiterConfig Tests ✔
│ ├─ ApplicationContext injected into method ✔
│ └─ Spring @Beans injected into fields ✔
├─ CatInterfaceDefaultMethodsTests ✔
│ ├─ autowiredParameterWithParameterizedList(List) ✔
│ └─ autowiredParameterWithGenericBean(Character) ✔
├─ DogInterfaceDefaultMethodsTests ✔
│ ├─ autowiredParameterWithParameterizedList(List) ✔
│ └─ autowiredParameterWithGenericBean(Character) ✔
├─ CatTests ✔
│ ├─ autowiredFields() ✔
│ └─ autowiredParameterByTypeForSingleGenericBean(Character) ✔
├─ DogTests ✔
│ ├─ autowiredFields() ✔
│ └─ autowiredParameterByTypeForSingleGenericBean(Character) ✔
├─ SpringExtensionTests ✔
│ ├─ autowiredFields() ✔
│ ├─ applicationContextInjectedIntoMethod(ApplicationContext) ✔
│ ├─ genericApplicationContextInjectedIntoMethod(GenericApplicationContext) ✔
│ ├─ autowiredParameterByTypeForSingleBean(Dog) ✔
│ ├─ autowiredParameterByTypeForPrimaryBean(Cat) ✔
│ ├─ autowiredParameterWithExplicitQualifier(Person) ✔
│ ├─ autowiredParameterWithImplicitQualifierBasedOnParameterName(Person) ✔
│ ├─ autowiredParameterAsJavaUtilOptional(Optional) ✔
│ ├─ autowiredParameterThatDoesNotExistAsJavaUtilOptional(Optional) ✔
│ ├─ autowiredParameterThatDoesNotExistButIsNotRequired(Number) ✔
│ ├─ autowiredParameterOfList(List) ✔
│ ├─ valueParameterWithPrimitiveType(int) ✔
│ ├─ valueParameterFromPropertyPlaceholder(Integer) ✔
│ ├─ valueParameterFromDefaultValueForPropertyPlaceholder(Boolean) ✔
│ ├─ valueParameterFromSpelExpression(String) ✔
│ ├─ valueParameterFromSpelExpressionWithNestedPropertyPlaceholder(String) ✔
│ └─ junitAndSpringMethodInjectionCombined(Cat, TestInfo, ApplicationContext, TestReporter) ✔
├─ SpringJUnit5AutowiredConstructorInjectionTests ✔
│ ├─ applicationContextInjected() ✔
│ ├─ propertyPlaceholderInjected() ✔
│ └─ beansInjected() ✔
├─ SpringJUnit5ConstructorInjectionTests ✔
│ ├─ applicationContextInjected() ✔
│ ├─ propertyPlaceholderInjected() ✔
│ ├─ testInfoInjected() ✔
│ └─ beansInjected() ✔
├─ MultipleWebRequestsSpringExtensionTests ✔
│ ├─ getPerson42() ✔
│ └─ getPerson99() ✔
├─ Web SpringExtension Tests ✔
│ └─ springMvcTest(WebApplicationContext) ✔
└─ XmlSpringExtensionTests ✔
├─ autowiredFields() ✔
├─ applicationContextInjectedIntoMethod(ApplicationContext) ✔
├─ genericApplicationContextInjectedIntoMethod(GenericApplicationContext) ✔
├─ autowiredParameterWithExplicitQualifier(Person) ✔
├─ autowiredParameterWithImplicitQualifierBasedOnParameterName(Person) ✔
└─ autowiredParameterOfList(List) ✔
Test run finished after 2360 ms
[ 12 containers found ]
[ 0 containers skipped ]
[ 12 containers started ]
[ 0 containers aborted ]
[ 12 containers successful ]
[ 0 containers failed ]
[ 43 tests found ]
[ 0 tests skipped ]
[ 43 tests started ]
[ 0 tests aborted ]
[ 43 tests successful ]
[ 0 tests failed ]
BUILD SUCCESSFUL in 6s
In order to execute the tests within an IDE, simply run SpringExtensionTestSuite
as a JUnit 4 test class.