Stubbing a void method to throw an exception

This post will explain how to stub a public void method to throw an exception. Consider the below code Class1 package package1; public class Class1 { public void method1() { } } Class1Test package package1; import static org.junit.Assert.fail; import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; public class Class1Test { private Class1 class1; @Test public void testMethod1()…… Continue reading Stubbing a void method to throw an exception

Capturing multiple method arguments (using ArgumentCaptor) passed to a method in a consecutive call

This post explains how to capture multiple arguments passed to a method in a consecutive calls. Consider the below code Class2 package package10; public class Class2 { private int val; public void method2(int val) { this.val = val; } } Class1 package package10; public class Class1 { private Class2 class2; public void method1(int val) {…… Continue reading Capturing multiple method arguments (using ArgumentCaptor) passed to a method in a consecutive call

How to call real method using mock object

This post explains how to call real methods of an instance when writing junit test cases using mockito. import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.when; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class Class1Test { @Mock private Class1 class1; @Test public void testMethod1() { //Stubbed the method1 of Class1 to return some random…… Continue reading How to call real method using mock object