Questions tagged [junit]

Popular unit testing framework for Java and Scala. The latest version, JUnit 5, supports rich annotation-based and parameterized tests. Consider using in conjunction with the Java or Scala tag to indicate your use case.

JUnit is a popular unit testing framework for JVM languages (Java, Scala, Groovy, Kotlin, and others). The current version, JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage. Consider using in conjunction with the appropriate language tag (, , , and others) tag to indicate your use case. It is one of the most popular Java frameworks and is used in circa 30% of all projects.

The latest iteration, JUnit 5 (use tag ) adds Java 8 idioms, parameterized tests and comprehensive support for custom extensions.

There are JUnit implementations for other programming languages, which are called xUnit.

Kent Beck, Erich Gamma and David Saff created the unit testing framework influencing Java development heavily.

Grab JUnit's source code at GitHub or even fork it.

Related Links:

27795 questions
11
votes
3 answers

JUnit not working with Lombok - annotation processing doesn't seem to work for test classes

I have a problem with Lombok and JUnit. I am using IntelliJ Idea, the latest one, with Lombok plugin installed and annotation processing enabled. I have an entity class: @Data @Builder @AllArgsConstructor public class User { private String…
11
votes
4 answers

Mockito - Mockito cannot mock this class - IllegalArgumentException: Could not create type

I was trying to use a Mock class in a test of mine and getting this error: org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class com.example.Car. Mockito can only mock non-private & non-final classes. If you're not…
riorio
  • 6,500
  • 7
  • 47
  • 100
11
votes
3 answers

JUnit - How to test a method with different values?

I have a method and wish to test it with different values. My question is: how can I write a JUnit test that would test the same method with different values?
user722781
  • 121
  • 1
  • 1
  • 3
11
votes
1 answer

JUnit Local Test - 'Unresolved reference: test'

Expected Importing libraries such as androidx.test:core:1.2.0, androidx.test.ext:truth:1.2.0, com.google.truth:truth:0.44, and etc. into a local JUnit test class named ExampleUnitTest.kt. Observed Error Unresolved reference:…
AdamHurwitz
  • 9,758
  • 10
  • 72
  • 134
11
votes
1 answer

How to test data class on Kotlin?

I have data class'es in my Kotlin project, what I use for JSON Rest responses.. Example: data class WeatherResponse(val city: String, val temperature: Double, val humidity: Double) To fullfill code coverage constraints, I would like to write some…
Noam Silverstein
  • 819
  • 2
  • 12
  • 18
11
votes
3 answers

Jenkins JUnit Plugin reports a build as unstable even if test fails

So I am using the Jenknis JUnit parser plugin to parse the test results. I have a job which has just one test and it fails all the time. However, the JUnit plugin marks the job as unstable and not failed. Any reasons why? I have tried to set the…
Jason
  • 2,246
  • 6
  • 34
  • 53
11
votes
1 answer

How to get rid of "Could not initialize plugin: interface org.mockito.plugins.MockMaker" when launching JUnit with Mockito using OpenJDK 12

I've been migrating a project from Java 8 to Java 12. Everything went well except for unit tests. When I compile and launch tests with Maven, many tests fail with the following message: java.lang.IllegalStateException: Could not initialize plugin:…
Francois
  • 586
  • 2
  • 6
  • 19
11
votes
1 answer

Inline functions are causing errors in unit test code coverage report

I'm writing some unit tests for my Android app written in Kotlin, and I'm getting errors from the included inlined collection functions, in this case specifically sortedBy override fun onDaysSelected(dayOfWeekList: ArrayList) { …
Ben987654
  • 3,280
  • 4
  • 27
  • 50
11
votes
3 answers

Hamcrest matcher to compare two arrays

I have a method that do sort an array, and I want to test it, I want to compare its result with the expected one, of course that I can do it using a for loop, but i'm asking if there is a Hamcrest matcher to do the comparison I have a class class…
octopus
  • 319
  • 1
  • 4
  • 16
11
votes
5 answers

Test coverage for many AND/OR conditions in one statement

Example statement: if (conditionA && conditionB && conditionC && conditionD) { return true; } I could write unit tests for all 2^4 combinations, but that would easily go out of hand if more conditions are added. What should be my unit testing…
rajan
  • 157
  • 1
  • 2
  • 10
11
votes
1 answer

How to JUnit tests a @PreAuthorize annotation and its spring EL specified by a spring MVC Controller?

I've defined this method in my Spring MVC Controller : @RequestMapping(value = "{id}/content", method=RequestMethod.POST) @PreAuthorize("principal.user.userAccount instanceof T(com.anonym.model.identity.PedagoAccount) AND…
Nico
  • 3,430
  • 4
  • 20
  • 27
11
votes
1 answer

JUnit5 parameterized test with multiple method source

I have 4 tests each with its own method source but the only difference between them is one parameter, in each method I init the mocks in different way. Is there a way that I can pass multiple method source? Example: @ParameterizedTest …
telebog
  • 1,706
  • 5
  • 25
  • 34
11
votes
2 answers

When using JUnit5, I got a warning: "ClassNotFoundException: org.junit.platform.engine.support.filter.ExclusionReasonConsumingFilter"

I'm trying to use JUnit5. First, I added dependencies to maven project: org.junit.jupiter junit-jupiter-engine 5.3.0
ayaya
  • 163
  • 1
  • 1
  • 8
11
votes
2 answers

How to test a Grails Service that utilizes a criteria query (with spock)?

I am trying to test a simple service method. That method mainly just returns the results of a criteria query for which I want to test if it returns the one result or not (depending on what is queried for). The problem is, that I am unaware of how to…
user569825
  • 2,369
  • 1
  • 25
  • 45
11
votes
1 answer

How to list the slowest JUnit tests in a multi-module Maven build

How can I list the slowest JUnit tests in a multi-module Maven build? This should be accross all modules. A Hudson/Jenkins solution could also do.
Axel Fontaine
  • 34,542
  • 16
  • 106
  • 137