Test made using Mockito and JUnit

This commit is contained in:
Shubham Aggarwal 2016-11-28 11:03:13 +05:30
parent 115f70c917
commit de8038fe4d
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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet") @WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet")
public class FormServlet extends HttpServlet { public class FormServlet extends HttpServlet {
@ -19,9 +20,12 @@ public class FormServlet extends HttpServlet {
String weight = request.getParameter("weight"); String weight = request.getParameter("weight");
try { try {
Double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height)); double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
request.setAttribute("bmi", bmi); request.setAttribute("bmi", bmi);
PrintWriter out= response.getWriter();
out.write(String.valueOf(bmi));
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request, response); dispatcher.forward(request, response);
} catch (Exception e) { } 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) { private Double calculateBMI(Double weight, Double height) {
return weight / (height * height); return weight / (height * height);
} }
} }

View File

@ -1,34 +1,51 @@
package com.root; package com.root;
import org.junit.Before;
import org.junit.Test; 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.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; 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;
/** public class FormServletTest {
* Created by shubham on 27/11/16.
*/ @Mock
public class FormServletTest extends Mockito { HttpServletRequest request;
@Mock
HttpServletResponse response;
@Mock
RequestDispatcher requestDispatcher;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test @Test
public void testFormServlet() throws Exception { public void testFormServlet() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class); when(request.getParameter("height")).thenReturn("2");
HttpServletResponse response = mock(HttpServletResponse.class); when(request.getParameter("weight")).thenReturn("80");
when(request.getParameter("userName")).thenReturn("baeldung"); StringWriter sw = new StringWriter();
when(request.getParameter("userPlanet")).thenReturn("Mars"); PrintWriter pw = new PrintWriter(sw);
when(response.getWriter()).thenReturn(pw);
PrintWriter writer = new PrintWriter(new StringWriter());
when(response.getWriter()).thenReturn(writer);
new FormServlet().doPost(request, response); 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);
} }
} }