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
3189
votes
59 answers

How do I test a class that has private methods, fields or inner classes?

How do I use JUnit to test a class that has internal private methods, fields or nested classes? It seems bad to change the access modifier for a method just to be able to run a test.
MattGrommes
  • 11,974
  • 9
  • 37
  • 40
2271
votes
35 answers

How do you assert that a certain exception is thrown in JUnit tests?

How can I use JUnit idiomatically to test that some code throws an exception? While I can certainly do something like this: @Test public void testFooThrowsIndexOutOfBoundsException() { boolean thrown = false; try { foo.doStuff(); …
SCdF
  • 57,260
  • 24
  • 77
  • 113
906
votes
7 answers

How to verify that a specific method was not called using Mockito?

How to verify that a method is not called on an object's dependency? For example: public interface Dependency { void someMethod(); } public class Foo { public bar(final Dependency d) { ... } } With the Foo test: public class…
beluchin
  • 12,047
  • 4
  • 24
  • 35
535
votes
34 answers

Maven does not find JUnit tests to run

I have a maven program, it compiles fine. When I run mvn test it does not run any tests (under TESTs header says There are no tests to run.). I've recreated this problem with a super simple setup which I will include below as well as the output…
Aaron Silverman
  • 22,070
  • 21
  • 83
  • 103
510
votes
7 answers

Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll

What is the main difference between @Before and @BeforeClass and in JUnit 5 @BeforeEach and @BeforeAll @After and @AfterClass According to the JUnit Api @Before is used in the following case: When writing tests, it is common to find that…
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
489
votes
10 answers

Mockito: Trying to spy on method is calling the original method

I'm using Mockito 1.9.0. I want mock the behaviour for a single method of a class in a JUnit test, so I have final MyClass myClassSpy = Mockito.spy(myInstance); Mockito.when(myClassSpy.method1()).thenReturn(myResults); The problem is, in the…
Dave
  • 15,639
  • 133
  • 442
  • 830
481
votes
12 answers

JUnit 5: How to assert an exception is thrown?

Is there a better way to assert that a method throws an exception in JUnit 5? Currently, I have to use an @Rule in order to verify that my test throws an exception, but this doesn't work for the cases where I expect multiple methods to throw…
steventrouble
  • 6,641
  • 3
  • 16
  • 19
477
votes
8 answers

Mockito : how to verify method was called on an object created within a method?

I am new to Mockito. Given the class below, how can I use Mockito to verify that someMethod was invoked exactly once after foo was invoked? public class Foo { public void foo(){ Bar bar = new Bar(); bar.someMethod(); } } I…
mre
  • 43,520
  • 33
  • 120
  • 170
470
votes
23 answers

How to run test methods in specific order in JUnit4?

I want to execute test methods which are annotated by @Test in specific order. For example: public class MyTest { @Test public void test1(){} @Test public void test2(){} } I want to ensure to run test1() before test2() each time I run…
卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
447
votes
11 answers

Why doesn't JUnit provide assertNotEquals methods?

Does anybody know why JUnit 4 provides assertEquals(foo,bar) but not assertNotEqual(foo,bar) methods? It provides assertNotSame (corresponding to assertSame) and assertFalse (corresponding to assertTrue), so it seems strange that they didn't bother…
Chris B
  • 9,149
  • 4
  • 32
  • 38
442
votes
15 answers

JUnit test for System.out.println()

I need to write JUnit tests for an old application that's poorly designed and is writing a lot of error messages to standard output. When the getResponse(String request) method behaves correctly it returns a XML response: @BeforeClass public static…
Mike Minicki
  • 8,216
  • 11
  • 39
  • 43
432
votes
20 answers

How to test that no exception is thrown?

I know that one way to do it would be: @Test public void foo() { try { // execute code that you expect not to throw Exceptions. } catch(Exception e) { fail("Should not have thrown any exception"); } } Is there any cleaner way…
Ankit Dhingra
  • 6,534
  • 6
  • 31
  • 34
421
votes
2 answers

How to verify a method is called two times with mockito verify()

I want to verify if a method is called at least once through mockito verify. I used verify and it complains like this: org.mockito.exceptions.verification.TooManyActualInvocations: Wanted 1 time: But was 2 times. Undesired invocation:
Ahmad Beg
  • 4,285
  • 3
  • 14
  • 5
411
votes
5 answers

Conditionally ignoring tests in JUnit 4

OK, so the @Ignore annotation is good for marking that a test case shouldn't be run. However, sometimes I want to ignore a test based on runtime information. An example might be if I have a concurrency test that needs to be run on a machine with a…
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
397
votes
10 answers

How to run JUnit test cases from the command line

I would like to run JUnit test cases from the command line. How can I do this?
KK.
  • 4,137
  • 2
  • 18
  • 11
1
2 3
99 100