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
Category: Mockito
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
Mocking an interface
In this post under Mockito, I will show how to create a mock of an interface with an example Below is the complete example where I create a mock of java.util.List interface and call its method. Test Class 1 package package3; 2 3 import static org.junit.Assert.assertEquals; 4 import static org.mockito.Mockito.when; 5 6 import java.util.List; 7…… Continue reading Mocking an interface
Verifying the exact number of method invocations
This post explains how to verify that a method is executed desired number of times using Mockito framework. Below is the code of the class to be tested. Class1 code package package14; import java.util.List; public class Class1 { private List list; public void method1() { for(int i = 0; i < 10 ;i++) { list.add(i);…… Continue reading Verifying the exact number of method invocations
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
Mocking abstract classes
This post explain mocking abstract classes for testing purposes. Note 1) This feature is not available in pre 1.10.12 release 2) We can mock abstract class only if it has parameter less constructor Below is the example of how we can do it. ClassA (Abstract Class) package package2; public abstract class ClassA { public abstract…… Continue reading Mocking abstract classes
Verifying the order of multiple method invocations in a single instance
When multiple methods of single instance are invoked in an order in a class. We can verify the order of those method invocations using Mockito’s InOrder functionality. Consider the below code Class2 package package4; public class Class2 { public void method1() { System.out.println(“Class2 method1”); } public void method2() { System.out.println(“Class2 method2”); } public void method3()…… Continue reading Verifying the order of multiple method invocations in a single instance
Different ways of using thenReturn
This post shows 3 different ways of using thenReturn. 1) First way involves configuring the mock object to return only one return value as shown below when(class1.method1()).thenReturn(2); The above code instructs Mockito to return 2, whenever we call class1.method1 (once or more than once, the return value will always be 2) 2) Second way involves…… Continue reading Different ways of using thenReturn
Verifying the order of multiple instances method invocation
This post explains how to verify the order in which multiple instances are called. For example lets consider the below class Class2 package package5; public class Class2 { public void method1() { System.out.println(“Class2 method1”); } } Class3 package package5; public class Class3 { public void method1() { System.out.println(“Class3 method1”); } } Class4 package package5; public…… Continue reading Verifying the order of multiple instances method invocation
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