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
95
votes
5 answers

Spec. for JUnit XML Output

Where can I find the specification of JUnit's XML output. My goal is to write a UnitTest++ XML reporter which produced JUnit like output. See: "Unable to get hudson to parse JUnit test output XML" and "Hudson, C++ and UnitTest++"
Gilad Naor
  • 20,752
  • 14
  • 46
  • 53
95
votes
4 answers

Why is assertEquals(double,double) deprecated in JUnit?

I was wondering why assertEquals(double, double) is deprecated. I used import static org.junit.Assert.assertEquals; and I used JUnit 4.11. Below is my code: import org.junit.Test; import static org.junit.Assert.assertEquals; public class…
jsh6303
  • 2,010
  • 3
  • 24
  • 50
95
votes
5 answers

Can I delay a stubbed method response with Mockito?

I'm writing unit tests now. I need to simulate long-run method with Mockito to test my implementation's timeout handling. Is it possible with Mockito? Something like this: when(mockedService.doSomething(a, b)).thenReturn(c).after(5000L);
user961548
94
votes
14 answers

Running a single JUnit test in Eclipse

If I have a test suite with multiple tests, when I try to run a single unit test, either from the context menu of the code editor, or from the JUnit view, it seems to insist on always running the entire suite, rather than the single test. Is there a…
user14128
  • 2,547
  • 4
  • 20
  • 16
93
votes
6 answers

JUnit4 fail() is here, but where is pass()?

There is a fail() method in JUnit4 library. I like it, but experiencing a lack of pass() method which is not present in the library. Why is it so? I've found out that I can use assertTrue(true) instead but still looks unlogical. @Test public void…
Eugene
  • 59,186
  • 91
  • 226
  • 333
93
votes
11 answers

How to test validation annotations of a class using JUnit?

I need to test the validation annotations but it looks like they do not work. I am not sure if the JUnit is also correct. Currently, the test will be passed but as you can see the specified email address is wrong. JUnit public static void…
Jack
  • 6,430
  • 27
  • 80
  • 151
93
votes
3 answers

Setting up JUnit with IntelliJ IDEA

Familiar with Java but unfamiliar with IntelliJ, how does one "get started" with JUnit integration? Inspired by Looking for a tutorial on using JUnit with Intellij IDEA 9.x which didn't answer my questions and was for an older version of IntelliJ.
Max
  • 4,882
  • 2
  • 29
  • 43
93
votes
14 answers

Spring MVC Controllers Unit Test not calling @ControllerAdvice

I have a set of Controllers in the application and a class annotated as @ControllerAdvice which sets up certain data elements that are used in each of these controllers. I'm using Spring MVC 3.2 and have Junits for these controllers. When I run the…
Arun
  • 1,111
  • 2
  • 9
  • 6
92
votes
4 answers

Where do I configure log4j in a JUnit test class?

Looking at the last JUnit test case I wrote, I called log4j's BasicConfigurator.configure() method inside the class constructor. That worked fine for running just that single class from Eclipse's "run as JUnit test case" command. But I realize…
skiphoppy
  • 97,646
  • 72
  • 174
  • 218
92
votes
8 answers

How to use JUnit and Hamcrest together?

I can't understand how JUnit 4.8 should work with Hamcrest matchers. There are some matchers defined inside junit-4.8.jar in org.hamcrest.CoreMatchers. At the same time there are some other matchers in hamcrest-all-1.1.jar in org.hamcrest.Matchers.…
yegor256
  • 102,010
  • 123
  • 446
  • 597
92
votes
6 answers

How to write unit tests in Spark 2.0+?

I've been trying to find a reasonable way to test SparkSession with the JUnit testing framework. While there seem to be good examples for SparkContext, I couldn't figure out how to get a corresponding example working for SparkSession, even though it…
bbarker
  • 11,636
  • 9
  • 38
  • 62
92
votes
1 answer

Mockito - thenCallRealMethod() on void function

I have been running into a problem when trying to write a JUnit test case and am relatively new to Mockito. I have a function of a class that I am mocking, this function happens to be of a void return type. When calling this function from my mocked…
CRDamico
  • 923
  • 1
  • 6
  • 4
91
votes
5 answers

JUnit Testing Exceptions

I'm really new to java. I'm running some JUnit tests on a constructor. The constructor is such that if it is given a null or an empty string for one of its parameters, it's supposed to throw an exception. When I test this constructor in JUnit with…
papercuts
  • 1,428
  • 4
  • 15
  • 16
90
votes
7 answers

How to Re-run failed JUnit tests immediately?

Is there a way to have an JUnit Rule or something similar that gives every failing test a second chance, just by trying to run it once again. Background: I have a large Set of Selenium2-WebDriver tests written with JUnit. Due to a very aggressive…
Ralph
  • 118,862
  • 56
  • 287
  • 383
90
votes
13 answers

Surefire Maven plugin: "Corrupted STDOUT by directly writing to native stream in forked JVM"

My JUnit tests are failing when running them through Maven and the Surefire plugin (version information below). I see the error message: Corrupted STDOUT by directly writing to native stream in forked JVM 4. See FAQ web page and the dump file…
user118967
  • 4,895
  • 5
  • 33
  • 54