Test made using Mockito and JUnit

This commit is contained in:
Shubham Aggarwal 2016-11-28 11:03:13 +05:30
parent aa2285f3b4
commit 0be0117470
2 changed files with 38 additions and 24 deletions

View File

@ -7,6 +7,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet")
public class FormServlet extends HttpServlet {
@ -19,9 +20,12 @@ public class FormServlet extends HttpServlet {
String weight = request.getParameter("weight");
try {
Double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
request.setAttribute("bmi", bmi);
PrintWriter out= response.getWriter();
out.write(String.valueOf(bmi));
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {
@ -30,15 +34,8 @@ public class FormServlet extends HttpServlet {
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// do something else here
}
private Double calculateBMI(Double weight, Double height) {
return weight / (height * height);
}
}
}

View File

@ -1,34 +1,51 @@
package com.root;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.StringWriter;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Created by shubham on 27/11/16.
*/
public class FormServletTest extends Mockito {
public class FormServletTest {
@Mock
HttpServletRequest request;
@Mock
HttpServletResponse response;
@Mock
RequestDispatcher requestDispatcher;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testFormServlet() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
when(request.getParameter("height")).thenReturn("2");
when(request.getParameter("weight")).thenReturn("80");
when(request.getParameter("userName")).thenReturn("baeldung");
when(request.getParameter("userPlanet")).thenReturn("Mars");
PrintWriter writer = new PrintWriter(new StringWriter());
when(response.getWriter()).thenReturn(writer);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
when(response.getWriter()).thenReturn(pw);
new FormServlet().doPost(request, response);
assertTrue(request.getParameter("userName").contains("baeldung"));
verify(request).setAttribute("bmi", 20.0);
String result = sw.getBuffer().toString().trim();
assertEquals("20.0", result);
}
}