编写 JUnit 测试用例请求调度程序时出错 [英] Error in writing JUnit test case request dispatcher

查看:29
本文介绍了编写 JUnit 测试用例请求调度程序时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为请求调度程序编写测试用例时遇到了一些错误.我的课

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:

  1. Mockito 提供的模拟是基于接口自动生成的,并使用 whenverify 等静态方法进行操作.Mockito 模拟是使用 Mockito.mock(或 @Mock 当且仅当您使用 MockitoJUnitRunnerMockitoAnnotations.initMocks).

  1. Mockito-provided mocks are automatically generated based on interfaces, and are manipulated with static methods like when and verify. Mockito mocks are created using Mockito.mock (or @Mock if and only if you use MockitoJUnitRunner or MockitoAnnotations.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆