20

I read an answer about Maven but I was wondering how I would achieve this task in Gradle - Executing JUnit 4 and JUnit 5 tests in a same build.

Currently my Gradle build only picks up tests with: import org.junit.jupiter.api.Test;

My problem is that I'm using @RunWith which needs JUnit4 to run but I want to execute it on a JUnit5 Vintage Engine.

How do I make my build such that I can run JUnit4 and JUnit5 together. Thanks.

Update: Now there is a native Mockito Junit Jupiter for JUnit 5 - https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter

Viv
  • 1,706
  • 1
  • 18
  • 27

1 Answers1

17

The junit5-migration-gradle project demonstrates how to execute tests based on JUnit 5 using Gradle. In addition, it showcases that existing JUnit 4 based tests can be executed in the same test suite as JUnit Jupiter based tests or any other tests supported on the JUnit Platform.

Basically it boils down to having both engines, Jupiter and Vintage, on the runtime class-path:

dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.2.0")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.2.0")
}

dependencies {
    testCompile("junit:junit:4.12")
    testRuntime("org.junit.vintage:junit-vintage-engine:5.2.0")
}
Sormuras
  • 8,491
  • 1
  • 38
  • 64
  • 3
    I believe there should be more than just this - my JUnit4 tests are still not executed on ```./gradlew clean test``` – Viv Jun 11 '18 at 23:56
  • 2
    Can you please post more information, like your `build.gradle` file or provide a link to a minimal sample project that shows the issue? – Sormuras Jun 12 '18 at 07:17
  • 5
    You also need to add this clause to your `build.gradle`: `test { useJUnitPlatform() }` Or, if you're using Kotlin and have a `build.gradle.kts`: `tasks.test { useJUnitPlatform() }` – Drew Stephens May 06 '20 at 13:33