编写JUnit测试用例请求分派器时出错 [英] Error in writing JUnit test case request dispatcher
问题描述
在为请求分发程序编写测试用例时遇到了一些错误. 我的课
I am facing some error while writing test case for Request dispatcher. My class
@Override
public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain)
throws IOException, ServletException
{
if(isMockAccountEnabled())
{
HttpServletRequest req = (HttpServletRequest)request;
String reqUrl = req.getRequestURI();
ApiUserDetails userDetails = userBean.getUserDetails();
HttpSession session = req.getSession();
if(isThisTestAccount(reqUrl, session))
{
log.info(userDetails);
log.debug("Entering Test acount flow for the request "+reqUrl);
RequestDispatcher dispatcher = req.getRequestDispatcher("/mock/" + EnumService.returnMockService(reqUrl));
dispatcher.forward(request, resp);
}
}
}
编写测试用例
@Mock
private FilterChain chain;
@InjectMocks
private MockAccountFilter mockAccountFilter = new MockAccountFilter();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpSession session = new MockHttpSession();
@Test
public void filterRequestMockFirst()
throws Exception
{
MockRequestDispatcher dispatcher =new MockRequestDispatcher("/mock/ABCTEST");
when(request.getRequestDispatcher("/mock/ABCTEST")).thenReturn(dispatcher);
request.setRequestURI("/check/employee/123456/false");
mockAccountFilter.doFilter(request, response, chain);
Assert.assertTrue(request.getRequestURI().contains("/mock/ABCTEST"));
}
错误
when() requires an argument which has to be 'a method call on a mock'.
有人可以告诉我编写此测试用例的确切方法吗?
Can some one tell me the exact way of writing this test case.
推荐答案
我没有足够的信息告诉您编写此测试用例的确切方法",并且StackOverflow并不是获取大块代码的好地方代码已修复,但我可以告诉您为什么收到该消息. :)
I don't have enough information to tell you "the exact way of writing this test case", and StackOverflow isn't a good place to get large blocks of code fixed, but I can tell you why you're getting that message. :)
MockHttpServletRequest request = new MockHttpServletRequest();
这里有两种模拟"的感觉:
There are two senses of "Mock" going on here:
-
Mockito提供的模拟是基于接口自动生成的,并且可以使用静态方法(如
when
和verify
)进行操作.使用Mockito.mock
(或仅当使用MockitoJUnitRunner
或MockitoAnnotations.initMocks
时,才使用@Mock
)创建Mockito模拟.
Mockito-provided mocks are automatically generated based on interfaces, and are manipulated with static methods like
when
andverify
. Mockito mocks are created usingMockito.mock
(or@Mock
if and only if you useMockitoJUnitRunner
orMockitoAnnotations.initMocks
).
名称以单词"Mock"开头的完整类(例如MockHttpServletRequest
)实际上是整个类实现,与通过J2EE实际接收到的实现相比,它们更容易进行突变或更改.这些可以更准确地称为伪",因为它们是用于测试的简单接口实现,无法验证行为并且无法通过Mockito进行工作.您可以确定它们不是Mockito模拟,因为您可以使用new MockHttpServletRequest();
实例化它们.
Full classes with names starting with the word "Mock", like MockHttpServletRequest
, are actually entire class implementations that happen to be easier to mutate or change than ones you would actually receive through J2EE. These might more accurately be termed "Fake", because they are simple interface implementations for testing that do not verify behavior and do not work through Mockito. You can tell for sure that they're not Mockito mocks because you instantiate them with new MockHttpServletRequest();
.
例如,
FilterChain可能由Mockito提供. MockHttpServletRequest request
不是Mockito模拟,这就是为什么收到错误消息的原因.
FilterChain, for instance, will likely be provided by Mockito. MockHttpServletRequest request
is not a Mockito mock, which is why you're getting the error message you're getting.
您最好的选择是选择一种模拟或另一种模拟(哪种都可以),并确保您使用when
语句(如果选择Mockito)或诸如setRequestURI
的设置器正确准备了这些模拟. (如果您选择MockHttpSession
样式的模拟).
Your best bet is to pick one type of mock or the other—either will work—and make sure that you're preparing those mocks properly with the when
statement (if you choose Mockito) or setters like setRequestURI
(if you choose the MockHttpSession
-style mocks).
这篇关于编写JUnit测试用例请求分派器时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!