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
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
Streaming two json files in an order using JsonParserSequence
This post explains how to stream two files in a particular order Main Class import java.io.File; import java.io.IOException; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.util.JsonParserSequence; public class ProcessingDemo3 { public static void main(String[] args) throws JsonParseException, IOException { File file1 = new File(“jsondata.json”); File file2 = new File(“jsondata1.json”); JsonFactory jsonFactory = new…… Continue reading Streaming two json files in an order using JsonParserSequence
Reading JSON in a streaming way
This post explain how to stream a json file. We need to create an instance of JsonFactory, as shown below JsonFactory jsonFactory = new JsonFactory(); by which we can create a parser instance (i.e., JsonParser) to read the json file, as shown below JsonParser jsonParser = jsonFactory.createParser(file); Each element whether start and end of the…… Continue reading Reading JSON in a streaming way
Unit testing method exceptions
Sometime we have to test whether a method throws proper and required exceptions or not. One way of testing is placing the method call (to be tested) inside a try catch block, as shown below try { class2.method1(); fail(“Should have thrown exception”); } catch(IllegalArgumentException excep) { } catch(Exception excep) { fail(“Should have thrown IllegalArgumentException exception”);…… Continue reading Unit testing method exceptions
Setting timeout for test methods
Junit offers us the option to set timeout for test methods. If a test methods doesnt complete within specified time limit, an exception is thrown failing the test case. The use case scenario where this feature can be used is for example testing the performance of a method. The below code shows you how to…… Continue reading Setting timeout for test methods
Making JUnit ignore test methods
If we want JUnit to ignore certain test methods we can do that by using the @Ignore annotation applied on the methods that has to be ignored. The below test class tests a functionality of a calculator CalculatorTest package Package1; import static org.junit.Assert.assertEquals; import org.junit.Ignore; import org.junit.Test; @Ignore public class CalculatorTest { @Test public void…… Continue reading Making JUnit ignore test methods