我正在学习软件测试,因为我主修计算机科学。教授给了我们一个用Java编写的程序的源代码来测试它。我现在正在测试这个方法:
public static void createPanel(HttpServletRequest req, HttpServletResponse res, HttpSession hs) throws IOException
{
String panelName = req.getParameter("panelName");
String panelDescription = req.getParameter("panelDescription");
int employeeID = ((EmployeeProfile)hs.getAttribute("User Profile")).EmployeeID;
boolean result;
//Let's validate our fields
if(panelName.equals("") || panelDescription.equals(""))
result = false;
else
result = DBManager.createPanel(panelName, panelDescription, employeeID);
b = result;
//We'll now display a message indicating the success of the operation to the user
if(result)
res.sendRedirect("messagePage?messageCode=Panel has been successfully created.");
else
res.sendRedirect("errorPage?errorCode=There was an error creating the panel. Please try again.");
}
我使用 Eclipse 与 JUnit 和mockito 来测试所有方法,包括这个方法。对于这一特定方法,我想检查程序是否重定向到一个位置或另一个位置,但我不知道该怎么做。你有什么主意吗?谢谢。
最佳答案
实际上,您可以使用 Mockito 和 ArgumentCaptor 轻松实现它:
@RunWith(MockitoJUnitRunner.class)
public class MyTest {
@Mock
private HttpServletResponse response
...
@Test
public void testCreatePanelRedirection(){
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
YourClass.createPanel(request, response, session);
verify(response).sendRedirect(captor.capture());
assertEquals("ExpectedURL", captor.getValue());
}
}
关于java - 如何知道 HttpServletResponse 重定向到哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33160208/