Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exemplo de teste de servlet usando mocks
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.junit.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.mockito.Mockito;
import static org.mockito.Mockito.when;
public class IMCServletTest {
HttpServletRequest req;
HttpServletResponse res;
StringWriter sw;
@Before
public void init() {
try {
req = Mockito.mock(HttpServletRequest.class);
res = Mockito.mock(HttpServletResponse.class);
sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
when(res.getWriter()).thenReturn(pw);
}
catch (Exception e) {}
}
@Test
public void testDoGet() {
try {
when(req.getParameter("peso")).thenReturn("82");
when(req.getParameter("altura")).thenReturn("1.80");
new IMCServlet().doGet(req,res);
assertEquals("IMC: 25.3\n", sw.toString());
}
catch (Exception e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.