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
28,065
questions
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 ...
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) {
...
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 ...
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 ...
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:
...
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())....
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 =...
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 ...
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 ...
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 ...
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 ...
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:
...
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 ...
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)...