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
2,768
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.
4
votes
1
answer
16k
views
Why are my mocked methods not called when executing a unit test?
Preface:
This question and answer are intended as a canonical answer to
the majority of questions that arise due to misuse of Mockito or
misunderstanding how Mockito works and interacts with unit ...
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 ...
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 ...
401
votes
11
answers
523k
views
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?
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 ...
390
votes
29
answers
608k
views
How to mock a final class with mockito
I have a final class, something like this:
public final class RainOnTrees{
public void startRain(){
// some code here
}
}
I am using this class in some other class like this:
public ...
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 ...
304
votes
25
answers
314k
views
Injecting Mockito mocks into a Spring bean
I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on ...
2
votes
2
answers
1k
views
java DateTimeFormatterBuilder fails on testtime [duplicate]
I have a simple jUnit test for DateTimeFormatterBuilder.
At runtime it works, when some String comes on Spring-MVC hanlder (@RequestParam)
At testtime it fails with the same String value.
Tested ...
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 ...
121
votes
10
answers
95k
views
Running junit tests in parallel in a Maven build?
I'm using JUnit 4.4 and Maven and I have a large number of long-running integration tests.
When it comes to parallelizing test suites there are a few solutions that allow me to run each test method ...
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 ...
238
votes
19
answers
131k
views
How to test methods that call System.exit()?
I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing these cases causes JUnit to terminate! Putting the method calls in a new Thread doesn't seem to help, ...
249
votes
7
answers
418k
views
When do Java generics require <? extends T> instead of <T> and is there any downside of switching?
Given the following example (using JUnit with Hamcrest matchers):
Map<String, Class<? extends Serializable>> expected = null;
Map<String, Class<java.util.Date>> result = null;
...