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
Using ArgumentCaptor class
This post explains how to capture arguments passed to mocked methods when testing. Suppose we have classes as shown below class Class1 { private Class2 class2; public void method1(int val) { val = val * val; class2.method2(val); } } class Class2 { private int val; public void method2(int val) { this.val = val; } }…… Continue reading Using ArgumentCaptor class
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
How to read a json file in java
Create a txt file named jsondata.txt with the below data { “firstName”: “Duke”, “lastName”: “Java”, “age”: 18, “streetAddress”: “100 Internet Dr”, “city”: “JavaTown”, “state”: “JA”, “postalCode”: “12345”, “phoneNumbers”: [ { “Mobile”: “111-111-1111” }, { “Home”: “222-222-2222” } ] } Below is the java code which will read the above text file Main Code package objectmodel;…… Continue reading How to read a json file in java