Created
November 14, 2019 15:38
-
-
Save mtov/5372c7ffebe79c21768d0b1290c409b1 to your computer and use it in GitHub Desktop.
Exemplo de teste de servlet usando mocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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