Mockito: Mock private field initialization

Mockito comes with a helper class to save you some reflection boiler plate code: import org.mockito.internal.util.reflection.Whitebox; //… @Mock private Person mockedPerson; private Test underTest; // … @Test public void testMethod() { Whitebox.setInternalState(underTest, “person”, mockedPerson); // … } Update: Unfortunately the mockito team decided to remove the class in Mockito 2. So you are back to … Read more

How to run all tests belonging to a certain Category in JUnit 4

I found out one possible way to achieve what I want, but I don’t consider this to be the best possible solution as it relies on ClassPathSuite library that is not part of JUnit. I define the test suite for slow tests like this: @RunWith(Categories.class) @Categories.IncludeCategory(SlowTests.class) @Suite.SuiteClasses( { AllTests.class }) public class SlowTestSuite { } … Read more

How do I Dynamically create a Test Suite in JUnit 4?

To create a dynamic test suite, you need to use the @RunWith annotation. There are two common ways to use it: @RunWith(Suite.class) This allows you to specify, which classes compose the test suite in question. This is equivalent to the JUnit 3 style: import junit.framework.TestSuite; import junit.framework.TestCase; public final class MasterTester extends TestCase { public … Read more

Changing names of parameterized tests

This feature has made it into JUnit 4.11. To use change the name of parameterized tests, you say: @Parameters(name=”namestring”) namestring is a string, which can have the following special placeholders: {index} – the index of this set of arguments. The default namestring is {index}. {0} – the first parameter value from this invocation of the … Read more

Getting “NoSuchMethodError: org.hamcrest.Matcher.describeMismatch” when running test in IntelliJ 10.5

Make sure the hamcrest jar is higher on the import order than your JUnit jar. JUnit comes with its own org.hamcrest.Matcher class that is probably being used instead. You can also download and use the junit-dep-4.10.jar instead which is JUnit without the hamcrest classes. mockito also has the hamcrest classes in it as well, so … Read more

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

It depends on the JUnit version and what assert libraries you use. For JUnit5 and 4.13 see answer https://stackoverflow.com/a/2935935/2986984 If you use assertJ or google-truth, see answer https://stackoverflow.com/a/41019785/2986984 The original answer for JUnit <= 4.12 was: @Test(expected = IndexOutOfBoundsException.class) public void testIndexOutOfBoundsException() { ArrayList emptyList = new ArrayList(); Object o = emptyList.get(0); } Though answer … Read more

@RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this)

MockitoJUnitRunner gives you automatic validation of framework usage, as well as an automatic initMocks(). The automatic validation of framework usage is actually worth having. It gives you better reporting if you make one of these mistakes. You call the static when method, but don’t complete the stubbing with a matching thenReturn, thenThrow or then. (Error … Read more