Skip to main content
The 2024 Developer Survey results are live! See the results

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.

3229 votes
59 answers
1.3m views

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.
2310 votes
35 answers
2.0m views

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 ...
SCdF's user avatar
  • 58.7k
947 votes
7 answers
659k views

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) { ...
beluchin's user avatar
  • 12.5k
552 votes
35 answers
493k views

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 ...
Aaron Silverman's user avatar
532 votes
12 answers
794k views

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 ...
steventrouble's user avatar
524 votes
7 answers
392k views

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: ...
Evgenij Reznik's user avatar
509 votes
11 answers
391k views

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())....
Dave's user avatar
  • 17.4k
499 votes
8 answers
989k views

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 =...
mre's user avatar
  • 44k
478 votes
22 answers
497k views

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 ...
卢声远 Shengyuan Lu's user avatar
457 votes
20 answers
617k views

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 ...
Ankit Dhingra's user avatar
449 votes
15 answers
379k views

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 ...
Mike Minicki's user avatar
  • 8,366
449 votes
11 answers
204k views

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 ...
Chris B's user avatar
  • 9,199
441 votes
2 answers
594k views

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: ...
Ahmad Beg's user avatar
  • 4,485
416 votes
5 answers
147k views

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 ...
Andrzej Doyle's user avatar
407 votes
9 answers
234k views

How to capture a list of specific type with mockito

Is there a way to capture a list of specific type using mockitos ArgumentCaptore. This doesn't work: ArgumentCaptor<ArrayList<SomeType>> argument = ArgumentCaptor.forClass(ArrayList.class)...
Andreas Köberle's user avatar

15 30 50 per page
1
2 3 4 5
1871