Search code examples
servletsmockitojunit4

JUnit testing with mockito


I have this filter class and need to have code coverage as high as possible while testing with junit.

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String userId= request.getHeader(Constants.USER_ID);

    if (StringUtils.isEmpty(userId)) {
        LOGGER.error("User Id is missing in request header.");
        isAuthorized = false;
    } 

    if (!isAuthorized) {
        LOGGER.warn("Authorization failed: User ID =[{}] is not authorized to access.", userId);
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().write(errorMsg);
    } else {
        filterChain.doFilter(request, response);
    }
}

And test class:

@RunWith(MockitoJUnitRunner.class)
public class SampleFilterTest {

    @Mock
    FilterConfig filterConfig;

    ServletRequest servletRequest;
    ServletResponse servletResponse;
    HttpServletRequest request;

    @Mock
    FilterChain filterChain;

    @Mock
    HttpServletResponse httpServletResponse;


    @InjectMocks
    SampleFilter sampleFilter;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void init() throws ServletException {
        sampleFilter.init(filterConfig);
    }

    @Test
    public void doFilter() throws IOException, ServletException{
        //when(request.getHeader(Constants.USER_ID)).thenReturn("batman");
        sampleFilter.doFilter(servletRequest, servletResponse, filterChain);
    }

    @Test
    public void destroy() {
        sampleFilter.destroy();
    }
}

When I run doFilter() it returns NullPointerException at

String userId= request.getHeader(Constants.USER_ID);

line.

How do I avoid this?

I need to call this method and execute whatever is inside to provide required code coverage.


Solution

  • The problem is that

    HttpServletRequest request = (HttpServletRequest) servletRequest;
    

    is casting to null because you pass in a ServletRequest which wont cast to HttpServletRequest

    Mock the actual type you are trying to casting it to within the method under test.

    For example

    //...
    
    @Test
    public void doFilter() throws IOException, ServletException {
        //Arrange
        //mock HttpServletResponse so cast does not fail
        ServletRequest request = mock(HttpServletRequest.class);
        when(request.getHeader(Constants.USER_ID)).thenReturn("batman");
    
        ServletResponse response = mock(HttpServletResponse.class);
        //setup response as neded. Looks like `.getWriter().write(errorMsg);` needs mocking
    
        //Act
        sampleFilter.doFilter(request, response, filterChain);
    
        //Assert
        //...
    }