Thursday, 3 March 2016

                                         Test Case From Sample Json in Android 


  Save the sample Json in text and save in Assert folder.
 Exp : sample.txt

 {
    "success": true,
    "data": {
             "message": "Document describes test cases that need to be executed to verify that the         specification has been implemented",
             "isConnectDevice": "Android",
             "requiresSerialNumber": true,
             "requiresDateOfPurchase": true
            }

}


sample test class as ResponseTest :

public class ResponseTest extends InstrumentationTestCase {
 
    public void testSummaryResponseObject() {
        try {
            StringBuilder sb = new StringBuilder();
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(getInstrumentation().getContext().getResources().getAssets().open("sample.txt")));
                String mLine = reader.readLine();
                while (mLine != null) {
                    sb.append(mLine);
                    mLine = reader.readLine();
                }

                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "Parsed Data : " + sb.toString());

            Data response = (ProductMetaData) mProductAsset.getResponseData(new JSONObject(sb.toString()));
            PojoData mResponseData = response.getData();
            assertNotNull(mResponseData);

            PojoData pojoData = sedataObject(mResponseData);
            TestAssertionOnResponse(mResponseData, pojoData);
        } catch (Exception e) {
            Log.d(TAG, "IO " + e);
        }
    }

    private void TestAssertionOnResponse(final PojoData mResponseData, final PojoData pojoData) {
        assertEquals(mResponseData.getMessage(), pojoData.getMessage());
        assertEquals(mResponseData.getisConnectDevice(), pojoData.getisConnectDevice();
        assertEquals(mResponseData.getrequiresSerialNumber(), pojoData.getrequiresSerialNumber());
        assertEquals(mResponseData.getrequiresDateOfPurchase(), pojoData.getrequiresDateOfPurchase());
       
    }

    private PojoData set]dataObject(final PojoData mResponseData) {
        PojoData pojoData = new PojoData();
        pojoData.setMessage(mResponseData.getMessage());
        pojoData.setIsConnectedDevice(mResponseData.getisConnectDevice());
        pojoData.setRequiresDateOfPurchase(mResponseData.getrequiresSerialNumber());
        pojoData.setExtendedWarrantyMonths(mResponseData.getrequiresDateOfPurchase());
        return pojoData;
    }
}