This post explains PushBackReader class in java. This reader allows us to put the data read from the input stream back into the stream and reread it again. The below explains how to use it Main Class package IO; import java.io.PushbackReader; import java.io.StringReader; public class PushBackReaderDemo { public static void main(String[] args) throws Exception {…… Continue reading PushBackReader Demo
Month: July 2016
Intercepting hibernate CRUD operations
This post explains how to intercept hibernate create, read, update and delete operations, so that we can add our own logic that has to be performaned before CRUD operations. The different use case where we need to intercept these hibernate operations are as follows 1) Every entity in the applications has common fields for example…… Continue reading Intercepting hibernate CRUD operations
UnMarshaling Json file
This post explains unmarshalling a object information stored in json format to java object . This can be achieved with the help of ObjectMapper provided by jackson databind package. To explain with an example we will use the below pojo objects User public class User { private Name name; private String ssn; private String sex;…… Continue reading UnMarshaling Json file
Marshaling Java Object
This post explains marshalling a java object to json format and storing it to a file. This can be achieved with the help of ObjectMapper provided by jackson databind package. To explain with an example we will use the below pojo objects User public class User { private Name name; private String ssn; private String…… Continue reading Marshaling Java Object
Retrieving JsonParser Settings
This post explains how to get json parser default settings. 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.JsonParser.Feature; public class ProcessingDemo2 { public static void main(String[] args) throws JsonParseException, IOException { File file = new File(“jsondata.json”); JsonFactory jsonFactory = new JsonFactory(); JsonParser jsonParser = jsonFactory.createParser(file); for(Feature feature : Feature.values()) { System.out.println(feature.name()…… Continue reading Retrieving JsonParser Settings
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
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