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
21
votes
4 answers

JUnit java.lang.NoSuchMethodError: org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances()

I was creating some JUnit test cases for my project app (Spring Boot) and encountered the following error stack below when I run my JUnit test cases: java.lang.NoSuchMethodError:…
Rico Sancho Abarro
  • 257
  • 2
  • 5
  • 12
21
votes
2 answers

How to use @MethodSource defined in other class in JUnit 5

Is there any way to use @MethodSource to use a method defined in other class ? e.g: Below code works, as the stringProvider method is defined in same class. @ParameterizedTest @MethodSource("stringProvider") void methodSourceFromOtherClass(String…
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
21
votes
3 answers

Missing org.junit.jupiter.params from JUnit5

I am trying to add parameterized tests into my Java program. I found the examples for JUnit 5, which I do have included. https://blog.codefx.org/libraries/junit-5-parameterized-tests/ The issue is I cannot add @ParameterizedTest because the…
AzzamAziz
  • 2,144
  • 1
  • 24
  • 34
21
votes
5 answers

What is the equivalent of ExternalResource and TemporaryFolder in JUnit 5?

According to the JUnit 5 User Guide, JUnit Jupiter provides backwards compatibility for some JUnit 4 Rules in order to assist with migration. As stated above, JUnit Jupiter does not and will not support JUnit 4 rules natively. The JUnit team…
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
21
votes
4 answers

Gradle Jacoco and JUnit5

We just ported our unit tests to JUnit5. Realizing that this is still rather early adoption with little hints on google. The most challenging was to get jacoco code coverage for the Junit5 tests which we use on jenkins. Since this took me almost a…
C. Ledergerber
  • 261
  • 2
  • 6
20
votes
3 answers

Using @EnabledIf with spring.profiles.active property in Spring Environment within tests

According to the documentation (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/junit/jupiter/EnabledIf.html#expression--) you can use the @EnabledIf annotation on a test class or test method like…
vab2048
  • 1,065
  • 8
  • 21
20
votes
1 answer

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…
Viv
  • 1,706
  • 1
  • 18
  • 27
20
votes
6 answers

Parameterizing with array in Junit 5 (or other testing Java library) in smarter fashion

I'm trying to parameterize this test: @Test public void reverseQuote(double[] qsp) throws Exception { ...} It seems absurd to me that it doesn't exists some quick method to initialize array qsp like, for example,…
Lore
  • 1,286
  • 1
  • 22
  • 57
20
votes
3 answers

Properly set (system) properties in JUnit 5

We are using an approach similar to System Rules to handle (system) properties in our JUnit 4 tests. The main reason for this is to clean up the environment after each test, so that other tests do not inadvertently depend on possible side…
beatngu13
  • 7,201
  • 6
  • 37
  • 66
20
votes
5 answers

Including JUnit 5 dependency in IntelliJ IDEA

From jetbrains blog: IntelliJ IDEA supports the ability to actually run tests written for JUnit 5 – there’s no need to use the additional libraries (like the Gradle or Maven plugins for example), all you need is to include the JUnit 5…
Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
20
votes
3 answers

How to change tests execution order in JUnit5?

JUnit4 has @FixMethodOrder annotation which allows to use alphabetical order of test methods execution. Is there analogous JUnit5 mechanism?
Radek Postołowicz
  • 4,506
  • 2
  • 30
  • 47
19
votes
4 answers

Is there any special configuration to use SpringRunner with junit5?

My micro-service project based on spring-boot framework and all my unit test running with spring runner. @RunWith(SpringRunner.class) adding this annotations, imports the following library: import…
Ilan Miller
  • 323
  • 1
  • 3
  • 11
19
votes
2 answers

Equivalent for @RunWith(JUnitPlatform.class) for JUnit5

In my project I'm doing some cleanup and decided to move everything to JUnit5. Till this time, I was using @RunWith(JUnitPlatform.class) Now I want to migrate it to @ExtendWith. Is there any equivalent for this JUnitPlatform.class in JUnit5?
Szymon Żak
  • 491
  • 1
  • 3
  • 17
19
votes
3 answers

Use pure Kotlin function as Junit5 methodsource

I am curious if in the Kotlin, in the parameterized tests in Junit5, i can use the method outside the class as the @MethodSource. I know about 2 ways to use @MethodSource in Kotlin - companion object and…
Failed
  • 228
  • 2
  • 8
19
votes
2 answers

JUnit 5: Inject spring components to Extension (BeforeAllCallback / AfterAllCallback)

tl;dr: How can I instantiate a custom data provider as a Spring component before all tests run? Is there a smart way to inject Spring components into a custom JUnit Jupiter extension that implements BeforeAllCallback? The beforeAll method should…
froehli
  • 904
  • 1
  • 11
  • 35