Marshalling and Unmarshalling java class

1) First we need to create jaxb context as shown below. A instance of this class is always required, as it is through this class we get marshaller and unmarshaller objects JAXBContext jaxbContext = JAXBContext.newInstance(Country.class); An instance of JAXBContext is thread safe but it is heavy weight object. So it is recommended to reuse the…… Continue reading Marshalling and Unmarshalling java class

How to use SequenceInputStream

SequenceInputStream reads data from multiple inputstreams in an order. It starts with first inputstream and once it is done reading the first inputstream, it starts reading data from the next available inputstream. Two constructors are available for creating an instance of this stream. First one being SequenceInputStream(InputStream s1, InputStream s2) The above constructors limits us…… Continue reading How to use SequenceInputStream

Updating an existing json data

This post explain how we can update an existing json data stored in a file. For example purpose lets create a json file named jsondata1.txt with the following data jsondata1.txt { “firstName”:”John”, ”lastName”:”McClane”, ”age”:”28″, ”address”:{ “street”:”street1″, ”city”:”city1″, ”state”:”state1″, ”country”:”country1″, ”postalCode”:”12345″ }, “Phones”:[{“Mobile”:”111-111-1111″},{“Home”:”222-222-2222″}] } The code is as shown below package objectmodel; import java.io.FileReader; import java.io.FileWriter;…… Continue reading Updating an existing json data

How to write json data to a file

This post explains how to write json data to a file. We will store the below json structure in the file { “firstName”:”John”, ”lastName”:”McClane”, ”age”:”28″, ”address”:{ “street”:”street1″, ”city”:”city1″, ”state”:”state1″, ”country”:”country1″, ”postalCode”:”12345″ }, “Phones”:[{“Mobile”:”111-111-1111″},{“Home”:”222-222-2222″}] } Note: The output will not be formatted package objectmodel; import java.io.FileWriter; import javax.json.Json; import javax.json.JsonArrayBuilder; import javax.json.JsonBuilderFactory; import javax.json.JsonObject; import javax.json.JsonObjectBuilder;…… Continue reading How to write json data to a file

How to call real method using mock object

This post explains how to call real methods of an instance when writing junit test cases using mockito. import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.when; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class Class1Test { @Mock private Class1 class1; @Test public void testMethod1() { //Stubbed the method1 of Class1 to return some random…… Continue reading How to call real method using mock object