Test made using Mockito and JUnit
This commit is contained in:
parent
aa2285f3b4
commit
0be0117470
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue