Pages

Friday 1 April 2016

Mockito


 1 : verify 
 
 //Let's import Mockito statically so that the code looks clearer
 import static org.mockito.Mockito.*;
 
 //mock creation
 List mockedList = mock(List.class);
 
 //using mock object
 mockedList.add("one");
 mockedList.clear();
 
 //verification
 verify(mockedList).add("one");
 verify(mockedList).clear();
 
 
 2 :  Argument matchers
 
 //stubbing using built-in anyInt() argument matcher
 when(mockedList.get(anyInt())).thenReturn("element");
 
 //stubbing using hamcrest (let's say isValid() returns your own hamcrest matcher):
 when(mockedList.contains(argThat(isValid()))).thenReturn("element");
 
 //following prints "element"
 System.out.println(mockedList.get(999));
 
 //you can also verify using an argument matcher
 verify(mockedList).get(anyInt());
 
 Argument matchers allow flexible verification or stubbing.
 
 3 : Verifying exact number of invocations / at least x / never
 
 //using mock 
 mockedList.add("once");
 
 mockedList.add("twice");
 mockedList.add("twice");
 
 mockedList.add("three times");
 mockedList.add("three times");
 mockedList.add("three times");
 
 //following two verifications work exactly the same - times(1) is used by default
 verify(mockedList).add("once");
 verify(mockedList, times(1)).add("once");
 
 //exact number of invocations verification
 verify(mockedList, times(2)).add("twice");
 verify(mockedList, times(3)).add("three times");
 
 //verification using never(). never() is an alias to times(0)
 verify(mockedList, never()).add("never happened");
 
 //verification using atLeast()/atMost()
 verify(mockedList, atLeastOnce()).add("three times");
 verify(mockedList, atLeast(2)).add("five times");
 verify(mockedList, atMost(5)).add("three times");

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;
    }
}