저는 JUnit 테스트에서 새로운데, if/else 문에 하나를 쓰는 데 문제가 있습니다. 나는 간단한 JUnit 테스트를 작성했지만, else 문 (체크를 위해 url을 체크 할 필요가있는 곳)을 건너 뛰고 마지막 else 문으로 간다. 여기에있는 문제는 URL 자체가 확인되지 않는다고 생각합니다. JUnit에서 나머지 if/else 문 (첫 번째/else가 테스트 된 경우)을 어떻게 테스트 할 수 있습니까? 내 Java 클래스 및 Junit 테스트를 추가하고 있습니다.jUnit if/else statement java
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
JSONObject jsonObject = null;
int assetId = 0;
int journeyId = 0;
Date selectedDate = new Date();
selectedDate.setTime(selectedDate.getTime() - 30 * 1000 * 60 * 60 * 24);
String[] comps = req.getRequestURI().split("/");
if (comps.length == 6)
{
assetId = Integer.parseInt(comps[5]);
jsonObject = getJourneys(assetId, selectedDate);
resp.getWriter().write(jsonObject.toString(4));
}
else if (comps.length == 7)
{
assetId = Integer.parseInt(comps[5]);
journeyId = Integer.parseInt(comps[6]);
jsonObject = getAssetJourneys(assetId, journeyId);
resp.getWriter().write(jsonObject.toString(4));
}
else
{
resp.getWriter().write("Entered url is in wrong format, please check it and try again");
}
}
@Test
public void testUrl() throws Exception
{
GPSHistoryService gpsService = mock(GPSHistoryService.class);
HttpServletRequest req = mock(HttpServletRequest.class);
HttpServletResponse resp = mock(HttpServletResponse.class);
PrintWriter pw = mock(PrintWriter.class);
when(resp.getWriter()).thenReturn(pw);
when(req.getRequestURI()).thenReturn("url");
JourneyRestApiServlet jras = new JourneyRestApiServlet(gpsService);
jras.doGet(req, resp);
verify(pw).write("Entered url is in wrong format, please check it and try again");
}
테스트 디버거를 가지고 콤프의 길이를 확인하기 위해 다른 JUnit 테스트
와 당신이 필요로하는 많은 junits 확인에
when
을 조롱. .. –"url"을 "/"로 나눠서 배열에 하나의 항목 만 가져옵니다. 당신은 일식에서 "Debug jUnit as ..."를 사용하여 이러한 것들을 쉽게 볼 수 있습니다. – luanjot
도와 주셔서 감사합니다. 감사합니다. – user2894127