Ich versuche, Mockito
Testfall zu schreiben, um die plantIDs
zu erhalten und einige Verarbeitung durchzuführen. Ich habe auch den Testfall hinzugefügt.Spott mit Mockito
Dies ist mein Testfall
@RunWith(MockitoJUnitRunner.class)
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;
@Mock List<String> plantResult;
@Test
public void TestGetPlantDetails() throws Exception
{
String plantID1= "23";
List<String> plantResult = new ArrayList<String>();
plantResult.add(plantID1);
Mockito.when(helperMock.getPlantIds()).thenReturn(plantResult);
URL url = new URL("**********/23");
conn=(HttpURLConnection)url.openConnection();
Mockito.when((HttpURLConnection)url.openConnection()).thenReturn(conn);
Mockito.when(conn.getResponseCode()).thenReturn(200);
input=conn.getInputStream();
Mockito.when(conn.getInputStream()).thenReturn(input);
ir=new InputStreamReader(input);
Mockito.when(new InputStreamReader((conn.getInputStream()))).thenReturn(ir);
buf=new BufferedReader(ir);
Mockito.when(new BufferedReader(new InputStreamReader((conn.getInputStream())))).thenReturn(buf);
String output=buf.readLine();
Mockito.when(buf.readLine()).thenReturn(output);
json=new JSONObject(output);
Mockito.when(new JSONObject(output)).thenReturn(json);
arr=json.getJSONArray("Data");
Mockito.when(json.getJSONArray("Data")).thenReturn(arr);
assertThat(output,is(notNullValue()));
List<PlantDetailsDTO> plantDetailsList=new ArrayList<PlantDetailsDTO>();
plantDetailsList=service.getPlantDetails();
}
}
Dies wirft einen Fehler in der Zeile, wo ich gelesen InputStream()
.Ich kann nicht geöffnet werden, weil Verbindung URL eine finale Klasse sein könnte. Ich bekomme einen Fehler in openConnection()
Linie auch .. Fehlender Methodenaufruf.
Ist Ihr 'helperMock'-Objekt' null', wenn Sie den Fehler erhalten? –
Null Pointer Ausnahme bei com.test.PlantDetailsServiceTest.TestGetPlantDetails (PlantDetailsServiceTest.java:57) – Rindha