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
115
votes
8 answers

Rollback transaction after @Test

First of all, I've found a lot of threads on StackOverflow about this, but none of them really helped me, so sorry to ask possibly duplicate question. I'm running JUnit tests using spring-test, my code looks like…
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90
113
votes
2 answers

How do I manage unit test resources in Kotlin, such as starting/stopping a database connection or an embedded elasticsearch server?

In my Kotlin JUnit tests, I want to start/stop embedded servers and use them within my tests. I tried using the JUnit @Before annotation on a method in my test class and it works fine, but it isn't the right behaviour since it runs every test case…
Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
111
votes
4 answers

Run single test from a JUnit class using command-line

I am trying to find an approach that will allow me to run a single test from a JUnit class using only command-line and java. I can run the whole set of tests from the class using the following: java -cp .... org.junit.runner.JUnitCore…
Kevin Jalbert
  • 3,115
  • 3
  • 26
  • 39
111
votes
1 answer

JUnit assertEquals(double expected, double actual, double epsilon)

Possible Duplicate: JUnit: assertEquals for double values Apparently the assertEquals(double expected, double actual) has been deprecated. The javadocs for JUnit are surprisingly lacking, considerings its wide use. Can you show me how to use the…
LuxuryMode
  • 33,401
  • 34
  • 117
  • 188
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
111
votes
7 answers

Mock a constructor with parameter

I have a class as below: public class A { public A(String test) { bla bla bla } public String check() { bla bla bla } } The logic in the constructor A(String test) and check() are the things I am trying to mock. I…
Shengjie
  • 12,336
  • 29
  • 98
  • 139
109
votes
8 answers

How to set JVM parameters for Junit Unit Tests?

I have some Junit unit tests that require a large amount of heap-space to run - i.e. 1G. (They test memory-intensive functionality for a webstart app that will only run with sufficient heap-space, and will be run internally on Win 7 64-bit machines…
amaidment
  • 6,942
  • 5
  • 52
  • 88
109
votes
17 answers

junit & java : testing non-public methods

JUnit will only test those methods in my class that are public. How do I do junit testing on the ones that are not (i.e., private, protected)? I can test them by not using junit, but I was wondering what the junit standard method was.
jbu
  • 15,831
  • 29
  • 82
  • 105
108
votes
26 answers

"Test events were not received" when run tests using Intellij

I have a Kotlin project and when I run my JUnit tests, I can't see tests execution result in IntelliJ and get this message instead: test events were not received I'm using this setup: macOS Mojave Intellij CE 2019.2 JDK 11.0.3 Kotlin…
Héctor
  • 24,444
  • 35
  • 132
  • 243
108
votes
3 answers

Java JUnit: The method X is ambiguous for type Y

I had some tests working fine. Then, I moved it to a different package, and am now getting errors. Here is the code: import static org.junit.Assert.*; import java.util.HashSet; import java.util.Map; import java.util.Set; import…
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
105
votes
4 answers

How to create unit tests easily in eclipse

I want to create unit tests easily by just selecting method. Is there a tool in eclipse that does that. It should support templates. I should be able to create positive test as well as negative tests.
Chotka
  • 1,278
  • 2
  • 8
  • 8
105
votes
10 answers

How can I find out if code is running inside a JUnit test or not?

In my code I need to do certain fixes only when it is run inside a JUnit test. How can I find out if code is running inside a JUnit test or not? Is there something like JUnit.isRunning() == true ?
Dr. Max Völkel
  • 1,780
  • 2
  • 17
  • 24
104
votes
5 answers

JUnit 4 Test Suites

How do I create test suites with JUnit 4? All the documentation I've seen doesn't seem to be working for me. And if I use the Eclipse wizard it doesn't give me an option to select any of the test classes I have created.
Adam Taylor
  • 7,534
  • 8
  • 44
  • 54
102
votes
10 answers

Assert regex matches in JUnit

Ruby's Test::Unit has a nice assert_matches method that can be used in unit tests to assert that a regex matches a string. Is there anything like this in JUnit? Currently, I do this: assertEquals(true, actual.matches(expectedRegex));
Josh Glover
  • 25,142
  • 27
  • 92
  • 129
102
votes
9 answers

What's the difference between failure and error in JUnit?

I'm running JUnit tests on a large code base, and I've been realizing that sometimes I get "Errors" while other times I get "Failures". What's the difference?
froadie
  • 79,995
  • 75
  • 166
  • 235