Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - 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);
                }
            }
        }

Test case written

@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"));

}

Error

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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-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).

  2. 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, 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.

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...