Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run JUnit5 and JUnit4 in same Gradle build?

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

like image 294
Viv Avatar asked Jun 05 '18 19:06

Viv


1 Answers

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")
}
like image 189
Sormuras Avatar answered Oct 12 '22 11:10

Sormuras