Mockito
のテストケースを書き込んで、plantIDs
を取得して何らかの処理を実行しようとしています。 私もテストケースを追加しました。Mockitoを使ったモック
これは私のテストケース
@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();
}
}
これは私が読んラインでエラーがスローされInputStream()
.Iを開くことができません接続URLは、最終的なクラスであるかもしれないので。 openConnection()
行にもエラーがあります。メソッド呼び出しがありません。
エラーが発生した場合、あなたの 'helperMock'オブジェクトは' null'ですか? –
com.test.PlantDetailsServiceTest.TestGetPlantDetails(PlantDetailsServiceTest.java:57)のヌルポインター例外 – Rindha