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
1
vote
1 answer

How to mock or prepare test methods in case we called private method inside the method?

While writing a testCase for Controller class,The private method that is getServiceContext(). has different object because one we are passing serviceContext from testclass and other object inside the controller class itself call itself.Due to this…
1
vote
1 answer

Unable to run unit test cases

I am writing test cases for my project using Junit5, when I run a test cases all the test cases passes, but some of them gives me below error in console WARNING: TestEngine with ID 'junit-jupiter' failed to execute…
Nisarg Patil
  • 1,509
  • 1
  • 16
  • 27
1
vote
1 answer

JUnit: share resource between test classes that uses same extension

My case: in my personal kotlin project I use JUnit 5 for testing. And I have three test groups: unit tests, integration tests and end-to-end tests. Unit tests works fine as they don't require anything. Integration tests require database connection,…
1
vote
1 answer

JUnit5 test a secured controller in using Spring security

I have a simple controller that should only be accessible in a secured context. In my application, I only whitelist other URLs, not /user. In the unit test for the single @GetResponse I get an Http Status of 401 instead of a 200. I expect this, as…
Dragonthoughts
  • 2,180
  • 8
  • 25
  • 28
1
vote
2 answers

Spring Boot: Inject mock in ApplicationRunner

I am trying to do integration tests which include the execution of an ApplicationRunner. I use @SpringBootTest, and when the context is initialized, the ApplicationRunner is automatically launched, which is okay. What I want to achieve is to inject…
Mr.Eddart
  • 10,050
  • 13
  • 49
  • 77
1
vote
1 answer

Spring Transactional - Ensure predictable behavior in tests and prod

I want to test the registration method of my UserService, which looks something like the below. @Transactional override fun register(userRegistration: UserRegistration): AuthDto { val user = userRegistration.toUserEntity() return try { …
avolkmann
  • 2,962
  • 2
  • 19
  • 27
1
vote
0 answers

Execute same testsuite in parallel using Junit5

In Junit 5, we can achieve parallel execution by specifying in junit-platform.properties file. Also we can run the same testsuite multiple times by using @TestTemplate but this is sequential run. Is there a way to combine both approaches. I want to…
DRP
  • 11
  • 3
1
vote
1 answer

Test custom constraint annotations with JUnit 5

Implementing a custom constraint annotation, like @MySize requires me testing it with unit tests to see if it functions correctly: public class MySizeTest { @Test public void noMinMax() { Dummy dummy = new Dummy(); // some…
neblaz
  • 683
  • 10
  • 32
1
vote
1 answer

Pass configuration to JUnit test via mvn command

I am writing some unit test that I want to pass environment parameters depending on the environment they are being executed. @Test public void databaseConfigTest() throws AutomatedTestFailureException { String connectionUrl =…
user2054833
  • 2,115
  • 7
  • 24
  • 39
1
vote
1 answer

Unable to have webEnvironment and repository access in the same Spring Boot Integration Test

Notes on the solution: @Transactional annotation and using lazy loading repository access seems to have been the root cause. Context: I'm writing Junit5 integration tests for a SpringBoot application (v2.2.5.RELEASE). It would start up SpringBoot…
1
vote
1 answer

How to create runnable jar for Selenium tests

I am using JUnit 5 and Selenium for my tests. I need to create a jar file, that when executed, opens the browser and performs the test cases. I've found from this video: How to Create Runnable Jar File From Selenium Project that using TestNG you…
Martina Carter
  • 409
  • 5
  • 18
1
vote
1 answer

How to specify matchers in Mockito unit test for a SpringBoot based service class

I am not able to execute unit test on a service class for a Spring Boot based web application using JUnit 5 and Mockito. Getting exception related to InvalidUseOfMatchersException. pom.xml
Nital
  • 5,784
  • 26
  • 103
  • 195
1
vote
1 answer

SonarQube not finding Kotlin Junit5 tests

I'm trying to set up a sample android studio project in Github which integrates also its CI connected to SonarQube. I'm having problems with sonarQube due to it says there is no test coverage applied. However there is, in kotlin and with Juni5,…
Jose M Lechon
  • 5,766
  • 6
  • 44
  • 60
1
vote
1 answer

How to prepare a nested data structure for a data-driven test in Karate?

I currently use junit5, wiremock and restassured for my integration tests. Karate looks very promising, yet I am struggling with the setup of data-driven tests a bit as I need to prepare a nested data structures which, in the current setup, looks…
u6f6o
  • 2,050
  • 3
  • 29
  • 54
1
vote
1 answer

How to test code that uses readline (System.`in`) in kotlin?

How do I test code in kotlin that does a readline like below: import org.junit.jupiter.api.* import org.junit.jupiter.api.Assertions.* import java.io.* fun foo() { val string="a b\nc d" var bais:…
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
1 2 3
99
100