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
Quartz Simple Example
Quartz schedular framework can be used to schedule jobs. This post explains how we can use it with a simple example. We need the below jars 1) c3p0-0.9.1.1.jar 2) log4j-1.2.16.jar 3) quartz-2.2.3.jar 4) quartz-jobs-2.2.3.jar 5) slf4j-api-1.7.7.jar 6) slf4j-log4j12-1.7.7.jar log4j.xml file in the classpath with the below configurations log4j.xml <?xml version=”1.0″ encoding=”UTF-8″?> <!DOCTYPE log4j:configuration SYSTEM “log4j.dtd”>…… Continue reading Quartz Simple Example
Achieving data binding with object model
This post explains how to achieve mapping of json data represented by an object model to particular java object. Here object model is tree like data structure which reads and stores the entire json content in memory. The difference between this post and other posts in Binding section is that latter posts explains marshalling and…… Continue reading Achieving data binding with object model
Creating a custom expiry policy factory
This post explains how to create a custom expiry policy for the cache. Below is the example Custom Expiry Policy 1 import java.util.concurrent.TimeUnit; 2 3 import javax.cache.expiry.Duration; 4 import javax.cache.expiry.ExpiryPolicy; 5 6 public class CustomTouchedExpiryPolicy implements ExpiryPolicy { 7 @Override 8 public Duration getExpiryForAccess() { 9 Duration duration = new Duration(TimeUnit.MINUTES, 2); 10 return duration;…… Continue reading Creating a custom expiry policy factory
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
JCache TouchedExpiryPolicy
This post explains TouchedExpiryPolicy with simple example. TouchedExpiryPolicy is an expiry policy used to inform cache to remove entries which has exceeded its presence since last modification (which includes creation, access and update) Main Code 1 import java.util.Iterator; 2 3 import javax.cache.Cache; 4 import javax.cache.Cache.Entry; 5 import javax.cache.CacheManager; 6 import javax.cache.Caching; 7 import javax.cache.configuration.Factory; 8…… Continue reading JCache TouchedExpiryPolicy
Saving entities in a batch
This post explains how to save large number of entities in one batch or one unit of work. First we need to set the below property in configuration file hibernate.jdbc.batch_size The value will be an integer, which indicates the number of insert statements to be grouped together as one unit of work or one batch.…… Continue reading Saving entities in a batch
Serialization of objects
This post explains how java objects can be serialized or deserialized in Spring framework Spring framework provides two interfaces and their default implementations as mentioned below Interfaces 1) Serializer 2) Deserializer Default Implementations 1) DefaultSerializer 2) DefaultDeserializer The default implementations internally uses java serialization to provide the serialization feature. In other words these implementations act…… Continue reading Serialization of objects
Global Scope and Engine Scope Bindings
This post explains about bindings and how we can use them to pass java objects to the script code. Java Scripting API provides map data structure which can be accessed, modified using Bindings Interface. The map entry’s key matches with the variable name in the script and their value can be a primitive value or…… Continue reading Global Scope and Engine Scope Bindings
Default Interface Example
Java 8 introduces a new feature called Default Interface. This feature allows interface creator to provide default implementations for methods, so that concrete classes that implementing the interface don’t have to provide implementations for those methods. The below code gives you an example of how to create default interface and how to use them. Interface…… Continue reading Default Interface Example