JUnit 4 @BeforeClass & @AfterClass when using Suites

Write a @BeforeClass method in the AllTests class which will be executed when the suite is started. public class MyTests1 { @BeforeClass public static void beforeClass() { System.out.println(“MyTests1.beforeClass”); } @Before public void before() { System.out.println(“MyTests1.before”); } @AfterClass public static void afterClass() { System.out.println(“MyTests1.AfterClass”); } @After public void after() { System.out.println(“MyTests1.after”); } @Test public void test1() … Read more

java: how to mock Calendar.getInstance()?

You can mock it using PowerMock in combination with Mockito: On top of your class: @RunWith(PowerMockRunner.class) @PrepareForTest({ClassThatCallsTheCalendar.class}) The key to success is that you have to put the class where you use Calendar in PrepareForTest instead of Calendar itself because it is a system class. (I personally had to search a lot before I found … Read more

Powermock – java.lang.IllegalStateException: Failed to transform class

Powermock 1.6.3 uses javassist 3.15.2-GA which does not support certain types. Using 3.18.2-GA javassist worked for me. You may want to override dependency in your project. <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.2-GA</version> </dependency> You may face another problem for which the solution lies here Mockito + PowerMock LinkageError while mocking system class Hope this helps.

Using assertArrayEquals in unit tests

This would work with JUnit 5: import static org.junit.jupiter.api.Assertions.*; assertArrayEquals(new int[]{1,2,3},new int[]{1,2,3}); This should work with JUnit 4: import static org.junit.Assert.*; import org.junit.Test; public class JUnitTest { /** Have JUnit run this test() method. */ @Test public void test() throws Exception { assertArrayEquals(new int[]{1,2,3},new int[]{1,2,3}); } } This is the same for the old JUnit … Read more

JUnit: Possible to ‘expect’ a wrapped exception?

As of JUnit 4.11 you can use the ExpectedException rule’s expectCause() method: import static org.hamcrest.CoreMatchers.*; // … @Rule public ExpectedException expectedException = ExpectedException.none(); @Test public void throwsNestedException() throws Exception { expectedException.expectCause(isA(SomeNestedException.class)); throw new ParentException(“foo”, new SomeNestedException(“bar”)); }

Unable to run JUnit test with PowerMockRunner

This is a bug that occurs when you use JUnit 4.12 and PowerMock < 1.6.1. The problem is solved in PowerMock 1.6.1. Please update your dependencies accordingly testCompile ‘junit:junit:4.12’, ‘org.powermock:powermock-core:1.6.1’, ‘org.powermock:powermock-module-junit4:1.6.1’, ‘org.powermock:powermock-api-mockito:1.6.1’ If you cannot upgrade PowerMock then you can use JUnit 4.11. testCompile ‘junit:junit:4.11’, ‘org.powermock:powermock-core:1.5.6’, ‘org.powermock:powermock-module-junit4:1.5.6’, ‘org.powermock:powermock-api-mockito:1.5.6’ Could you please add further lines of … Read more

Android unit test not mocked

JSON is bundled up with the Android SDK, so you’ll just be hitting a stub. You can pull in a JSON jar, which will provide real objects to use. To do this, you’ll need to add this to your build.gradle: testImplementation ‘org.json:json:20140107’ Alternatively, you can download and include the jar. testCompile files(‘libs/json.jar’) Note that the … Read more

Excluding a non param test in parameterized test class

JUnit 5 As of Junit 5.0.0 you can now annotate your test methods with @ParameterizedTest. So no need for inner classes. There are many ways to supply the arguments to the parameterized test apart from ValueSource as shown below. See the official junit user guide for details: import org.junit.jupiter.api.Test; import org.junit.jupiter.api.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; public class … Read more