Witam,
Mam dwa pyt dot. trestów MOCKująacych.
Dlaczego funkcja testDoGet, otrzymuje wyjątek AssertionFailedException, a nie NumberFormatException??
Gdy wypisuje wartości w działaniu programu DivideElement, to dostaję następująco:
Id to: asd
NumberFormatddException
Pyt2. Dla testu testDoGet 2, funkcja getStatus() zwraca 0??
public class DivideElement extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, IOException {
try {
String id = request.getParameter("id");
System.out.println("Id to: " + id);
int number = Integer.parseInt(id);
float result = 10 / number;
response.setStatus(200);
} catch(NumberFormatException ex) {
System.out.println("NumberFormatddException ");
response.sendError(501);
} catch(Exception ex) {
System.out.println("Exception ");
response.sendError(500);
}
}
}
public class DivideElementTest {
@Test (expected = NumberFormatException.class)
public void testDoGet() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
DivideElement instance = new DivideElement();
when(request.getParameter("id")).thenReturn("asd");
instance.doGet(request, response);
}
@Test
public void testDoGet2() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
DivideElement instance = new DivideElement();
when(request.getParameter("id")).thenReturn("2");
instance.doGet(request, response);
System.out.println("response.getStatus(): " + response.getStatus()); <-- dlaczego ta metoda zwraca 0
Assert.assertEquals(200,response.getStatus());
}
}