Commit 5ba42fdc authored by Jamie Tanna's avatar Jamie Tanna

Document JUnit4 + JUnit5 gotcha

Closes #766.
parent 12866b71
Pipeline #105202450 passed with stages
in 6 minutes and 44 seconds
---
title: "Gotcha: Running both JUnit4 and JUnit5 Together with Gradle"
description: "How to make sure your JUnit4 and JUnit5 Gradle tests work within the same project."
tags:
- blogumentation
- java
- gradle
- junit
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2019-12-23T21:57:33+0000
slug: "junit4-junit5-gotcha-together-gradle"
---
About a month ago, and earlier today, I found an issue with partially upgrading my Java projects from JUnit4 to JUnit5. I was in a halfway house between upgrading all my tests to JUnit5, so still had some JUnit4 tests, but found that my Gradle build wasn't running the new JUnit5 tests.
It turns out that to use the new JUnit test runner, you need to explicitly set it up - it makes sense when you think about it!
To get this working with Gradle, we need to pull in the `junit-vintage-engine` (via [_Executing legacy tests with JUnit Vintage_](https://docs.gradle.org/current/userguide/java_testing.html#executing_legacy_tests_with_junit_vintage)) by updating our `build.gradle` as such:
```groovy
test {
useJUnitPlatform()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
testCompileOnly 'junit:junit:4.12'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.1.0'
}
```
With this in place both my new JUnit5 and legacy JUnit4 tests were running correctly!
---
title: "Gotcha: Running both JUnit4 and JUnit5 Together with Maven"
description: "How to make sure your JUnit4 and JUnit5 Maven tests work within the same project."
tags:
- blogumentation
- java
- maven
- junit
license_code: Apache-2.0
license_prose: CC-BY-NC-SA-4.0
date: 2019-12-23T21:57:33+0000
slug: "junit4-junit5-gotcha-together-maven"
---
About a month ago, and earlier today, I found an issue with partially upgrading my Java projects from JUnit4 to JUnit5. I was in a halfway house between upgrading all my tests to JUnit5, so still had some JUnit4 tests, but found that my Gradle build wasn't running the new JUnit5 tests.
It turns out that to use the new JUnit test runner, you need to explicitly set it up - it makes sense when you think about it!
My solution to getting this working with Maven was via [the maven-surefire-plugin documentation for JUnit5](https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html), and adding the following to my `pom.xml`
```xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
```
With this in place both my JUnit5 and legacy JUnit4 tests were running correctly!
---
posts:
- https://www.jvt.me/posts/2019/12/23/junit4-junit5-gotcha-together-gradle/
- https://www.jvt.me/posts/2019/12/23/junit4-junit5-gotcha-together-maven/
- https://www.jvt.me/posts/2019/12/19/meetup-mf2-hfeed/
- https://www.jvt.me/posts/2019/12/17/xml-pretty-print/
- https://www.jvt.me/posts/2019/12/17/git-format-patch-apply-backport-replay-changes/
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment