Questions tagged [junit5]

Version 5 of the popular JUnit testing framework for the JVM. JUnit is a framework for writing repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit 5 is the 5th generation of the popular JUnit testing framework for the JVM based on Java 8 and compatible with Java 9, 10, and 11.

The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform. Furthermore, the platform provides a Console Launcher to launch the platform from the command line and build plugins for Gradle and Maven as well as a JUnit 4 based Runner for running any TestEngine on the platform.

JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform.

JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

JUnit 5 Resources

Frequently asked questions

Tags

  • Use the tag only for questions related to features provided by version 5 or migration to version 5.

  • Use the tag only for questions that are specific to JUnit Jupiter.

  • Use the tag for general questions related to JUnit.

3531 questions
510
votes
7 answers

Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

What is the main difference between @Before and @BeforeClass and in JUnit 5 @BeforeEach and @BeforeAll @After and @AfterClass According to the JUnit Api @Before is used in the following case: When writing tests, it is common to find that…
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
481
votes
12 answers

JUnit 5: How to assert an exception is thrown?

Is there a better way to assert that a method throws an exception in JUnit 5? Currently, I have to use an @Rule in order to verify that my test throws an exception, but this doesn't work for the cases where I expect multiple methods to throw…
steventrouble
  • 6,641
  • 3
  • 16
  • 19
299
votes
32 answers

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0

I've got a gradle FAILURE: ..."Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0." Case description: Attached to the project codebase the next libs: APP/build.gradle //(Required) Writing and executing…
Philipp Buhaievskiy
  • 3,217
  • 2
  • 10
  • 17
209
votes
4 answers

How to use Mockito with JUnit5

How can I use injection with Mockito and JUnit 5? In JUnit4 I can just use the @RunWith(MockitoJUnitRunner.class) Annotation. In JUnit5 is no @RunWith Annotation?
Daniel Käfer
  • 4,458
  • 3
  • 32
  • 41
156
votes
19 answers

Surefire is not picking up Junit 5 tests

I wrote a simple test method with JUnit 5: public class SimlpeTest { @Test @DisplayName("Some description") void methodName() { // Testing logic for subject under test } } But when I run mvn test, I…
Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
129
votes
46 answers

Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory

The problem Whenever I run my projects JUnit test (using JUnit 5 with Java 9 and Eclipse Oxygen 1.a) I encounter the problem that eclipse can't find any tests. The description Under the run configuration, eclipse can't even find the method which is…
ShadowDragon
  • 2,238
  • 5
  • 20
  • 32
120
votes
6 answers

In JUnit 5, how to run code before all tests

The @BeforeAll annotation marks a method to run before all tests in a class. http://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations But is there a way to run some code before all tests, in all classes? I want to ensure that…
Rob N
  • 15,024
  • 17
  • 92
  • 165
115
votes
19 answers

Junit 5 - No ParameterResolver registered for parameter

I can write up and execute Selenium script without any special test framework but I wanted to use Junit 5 (because we have dependency with other tools) and I have never seen such error org.junit.jupiter.api.extension.ParameterResolutionException…
Mike ASP
  • 2,013
  • 2
  • 17
  • 24
111
votes
3 answers

assertAll vs multiple assertions in JUnit5

Is there any reason to group multiple assertions: public void shouldTellIfPrime(){ Assertions.assertAll( () -> assertTrue(isPrime(2)), () -> assertFalse(isPrime(4)) ); } instead of doing this: public void…
Wilhelm Olejnik
  • 2,382
  • 3
  • 14
  • 21
108
votes
23 answers

Error "TestEngine with ID 'junit-vintage' failed to discover tests" with Spring Boot 2.2

I have a simple app using Spring Boot and Junit 5: When using Spring Boot 2.1 (e.g, 2.1.8 or 2.1.12), my unit tests run smoothly When using Spring Boot 2.2 (e.g., 2.2.2.RELEASE or 2.3.2.RELEASE) my unit tests fail with error message [ERROR]…
Ivan dal Bosco
  • 3,233
  • 4
  • 19
  • 24
96
votes
5 answers

How to replace WireMock @Rule annotation in JUnit 5?

I'm using WireMock in my tests and have such a line of code: @Rule public WireMockRule wireMockRule = new WireMockRule(8080); I want to switch to JUnit 5. So I added the next dependency (using…
IKo
  • 4,998
  • 8
  • 34
  • 54
92
votes
4 answers

When to use @RunWith and when @ExtendWith

My team and I have been working on a bunch of microservices using Spring boot. Since the services went through JUnit and Spring Boot upgrades (We're using now Spring Boot 2 and JUnit 5), different JUnit implemented by different devs, are now using…
AR1
  • 4,507
  • 4
  • 26
  • 42
87
votes
9 answers

Maven not running JUnit 5 tests

I'm trying to get a simple junit test running with maven but it is not detecting any tests. Where am I going wrong? The project directory Project -> src -> test-> java -> MyTest.java Results : Tests run: 0, Failures: 0, Errors: 0, Skipped:…
mogoli
  • 2,153
  • 6
  • 26
  • 41
77
votes
8 answers

JUnit 5 does not execute method annotated with BeforeEach

JUnit 5 does not invoke my method in a test class that is annotated with the @BeforeEach annotation, where I initialize some fields of the test object that are needed in the tests. When trying to access these fields inside a test method (method…
David
  • 1,672
  • 2
  • 13
  • 32
77
votes
5 answers

Difference between junit-jupiter-api and junit-jupiter-engine

What's the difference between maven modules junit-jupiter-api and junit-jupiter-engine? Is it necessary to include both dependencies in build.gradle? Do I need to provide both…
yanefedor
  • 2,132
  • 1
  • 21
  • 37
1
2 3
99 100