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.
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 ...
knittl's user avatar
  • 261k
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
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
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?
KK.'s user avatar
  • 4,187
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
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 ...
buttowski's user avatar
  • 4,867
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
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 ...
teabot's user avatar
  • 15.4k
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 ...
Markus Schnee's user avatar
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
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 ...
krosenvold's user avatar
  • 76.5k
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
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, ...
Chris Conway's user avatar
  • 55.8k
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; ...
Yishai's user avatar
  • 91.4k

15 30 50 per page
1
2 3 4 5
185