In this post under mockito, I will explain how to stub consecutive method calls with example. Consecutive method calls meaning sometimes we call same method multiple times. In that case we have to stub the method to return values consecutively. Below is the complete code for your reference Test Class 1 package package7; 2 3…… Continue reading Stubbing consecutive method calls
Tag: stubs
Stubbing a default interface method
In this post under Mockito, I will explain how to change a default interface method implementation for testing purposes with an example. For testing purposes I have created an interface as shown below Interface1 package package6; public interface Interface1 { public default int method1(int value) { return value * 2; } } The test case…… Continue reading Stubbing a default interface method
Stubbing final methods
This post explains how to stub a final method. The class to be tested is as shown below Class1 package package5; public class Class1 { public final int method1(int value) { return value * value; } } The test class is as shown below Test Class 1 package package5; 2 3 import org.junit.Before; 4 import…… Continue reading Stubbing final methods
Stubbing private methods
This post explains how to stub private methods of a class using PowerMock. Lets consider a class named “Class1” with the structure as shown below Classes to be tested package package3; public class Class1 { private int method1(int value1) { return value1 + 2; } public int method2(int value2) { return this.method1(value2); } } We…… Continue reading Stubbing private methods
Stubbing void methods to do nothing
This post explains how to change the implementation of void methods to do nothing for testing purposes. Below is the example Classes to be tested package package13; public class Class2 { public void method2(String value) { value = value + ” World”; } } package package13; public class Class1 { private Class2 class2; public String…… Continue reading Stubbing void methods to do nothing
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