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
76
votes
18 answers

Mockito gives UnfinishedVerificationException when it seems OK

Mockito appears to be throwing an UnfinishedVerificationException when I think I've done everything correctly. Here's my partial test case: HttpServletRequest req =…
Jonathan
  • 7,536
  • 4
  • 30
  • 44
72
votes
1 answer

Parameterized Test with two Arguments in JUnit 5 jupiter

How can I write a parameterized test with two arguments in JUnit 5 jupiter? The following does not work (compile error): @ParameterizedTest @ValueSource(strings = { "a", "b", "foo" }) @ValueSource(ints = { 1, 2, 3 }) public void test(String arg1,…
ruediste
  • 2,434
  • 1
  • 21
  • 30
72
votes
2 answers

How do I use Hamcrest with JUnit 5 when JUnit 5 doesn't have an assertThat() function?

To use Hamcrest with JUnit 4 we use an assertThat() function. However, JUnit 5 is no longer going to have an assertThat() function. How do I use Hamcrest without an assertThat()?
Max
  • 2,036
  • 2
  • 18
  • 27
67
votes
3 answers

Junit 5 with Spring Boot: When to use @ExtendWith Spring or Mockito?

I Have the following abstract unit test class that all my concrete unit test classes extend: @ExtendWith(SpringExtension.class) //@ExtendWith(MockitoExtension.class) @SpringBootTest( classes = PokerApplication.class, webEnvironment =…
java12399900
  • 1,485
  • 7
  • 26
  • 56
65
votes
5 answers

Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath in Junit 5

I got following execption when i tried to run test case in junit5: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project CRUD-App: Execution default-test of goal…
Bhuwan Prasad Upadhyay
  • 2,916
  • 1
  • 29
  • 33
64
votes
5 answers

What use is @TestInstance annotation in JUnit 5?

Can you give a simple explanation of @TestInstance annotation and how it is useful in JUnit 5? I think we can achieve the same effect probably by making our fields static.
Mahozad
  • 18,032
  • 13
  • 118
  • 133
63
votes
6 answers

How to use JUnit 5 with Gradle?

I am trying to use JUnit 5 with Gradle after I succeeded in running a JUnit 4 test. Expected result: Tthe JUnit 4 test gave a nice 'passed' in the output and an html report in build/reports/tests. Actual result: The JUnit 5 test as below does not…
PHPirate
  • 7,023
  • 7
  • 48
  • 84
55
votes
3 answers

Executing JUnit 4 and JUnit 5 tests in a same build

In Maven projects, I have some existing tests relying on JUnit 4. I cannot migrate these tests in JUnit 5 for multiple reasons. Essentially, some tests depend on a library which uses JUnit 4 runner and code migration may take time. I would like all…
davidxxx
  • 125,838
  • 23
  • 214
  • 215
54
votes
4 answers

What's the purpose of the JUnit 5 @Nested annotation

In JUnit 5, there is a new annotation: @Nested. I understand how the annotation works, I understand why we use nested classes, I just don't understand why we need to have nested test classes.
sab
  • 4,352
  • 7
  • 36
  • 60
50
votes
3 answers

junit5 MethodSource in nested class

I am working with junit5 and I want to create Parameterized Test in nested class. For example: class CardTest { @Nested class Cost { Stream cards() { return Stream.of( …
eugene-karanda
  • 760
  • 1
  • 6
  • 9
50
votes
4 answers

JUnit5 parameterized tests at class level

Is it possible to use JUnit5's parameterized new features to run test classes to receive test parameters instead of doing it at method level? With JUnit 4 a runner such as @RunWith(Parameterized::class) plus inheritance could be used to pass an…
IS1_SO
  • 865
  • 3
  • 10
  • 16
48
votes
12 answers

"No tests found for given includes" when running Gradle tests in IntelliJ IDEA

I cannot run tests via Gradle in IntelliJ IDEA because of "No tests found for given includes" error. How can I fix it? GradleTests import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; public class…
Arrovil
  • 865
  • 1
  • 8
  • 20
47
votes
4 answers

Simulation of Service using Mockito 2 leads to stubbing error

I try to simulate the behaviour of a class, using Mockito. This worked using Mockito 1.x. Migrating to JUnit 5 and Mockito 2 it seems not to work anymore. @ExtendWith(MockitoExtension.class) public class MockitoExample { static abstract class…
aschoerk
  • 3,333
  • 2
  • 15
  • 29
44
votes
4 answers

Generating display names for @ParameterizedTest in JUnit 5

I have a bunch of @ParameterizedTests that receive parameters from a @MethodSource with quite verbose toString() results (e.g. Selenium's WebDriver). These are used per default to compose the corresponding display names. From the JUnit 5 user…
beatngu13
  • 7,201
  • 6
  • 37
  • 66
44
votes
13 answers

java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException when trying run junit5 test with maven

When trying to run tests using command mvn test I receive an error: [ERROR] There was an error in the forked process [ERROR] java.lang.NoClassDefFoundError: org/junit/platform/commons/PreconditionViolationException [ERROR]…
kris82pl
  • 977
  • 3
  • 12
  • 21