jeudi 2 mars 2017

Mocking using Mockito

I am trying to write Mockito test case to get the plantIDs and perform some processing. Why does it throw error in the line I mentioned in comment. I have a call to Helper class method getPlantIDs(), i am just returning a list without the need to get from database.On stubbing i am just returning the plantIDs list.It throws me null pointer exception.

public class PlantDetailsServiceTest {
        @InjectMocks
        PlantDetailsService service;
        @Mock
        PlantDetailsHelper helperMock;
        @Mock
        HttpURLConnection conn;
        @Mock
        BufferedReader buf;
        @Mock
        InputStream input;
        @Mock
        InputStreamReader ir;
        @Mock 
        JSONObject json;
        @Mock
        JSONArray arr;

        @Test
        public void TestGetPlantDetails() throws Exception
        {
             List<String> plantResult= new ArrayList<String>(); 
             String plantID1= "PL001341";
             plantResult.add(plantID1);
             Mockito.when(helperMock.getPlantIds()).thenReturn(plantResult);
             //**WHY does it throw null pointer exception in above line**
             URL url = new URL("*********some URL***");
             url=new URL(url+plantResult.get(0));
             conn=(HttpURLConnection) url.openConnection();

             conn.setRequestMethod("GET");
             conn.setRequestProperty("Accept", "application/json");
             int code=conn.getResponseCode();

Aucun commentaire:

Enregistrer un commentaire