简体   繁体   中英

Mocked Class specified with Mockito is null when called in OncePerRequestFilterTest

I have the following Filter:

@Component
public class OriginFilter extends OncePerRequestFilter {

@Autowired
private CustomJdbcTokenStore tokenStore;

@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain)
        throws ServletException, IOException {
    final String requestTokenHeader = httpServletRequest.getHeader("Authorization");
    
    if (requestTokenHeader != null) {

        if(tokenStore.verifyUserAgentAndHostname(requestTokenHeader.substring(7), httpServletRequest.getHeader("User-Agent"), httpServletRequest.getRemoteAddr())){
            filterChain.doFilter(httpServletRequest, httpServletResponse);
        }else {
            httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }
    } else {
        logger.warn("Token is null");
}
}

I have the following Test:

But in there the mocked tokenStore is always Null, seems its taking it from the Instance in the Class itself but not the one specified with Mockito.

@RunWith(MockitoJUnitRunner.class)
public class OFilterTest {

@Mock
CustomJdbcTokenStore tokenStore;

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

@Test
public void 
CheckIfFilterPreventsDifferentOriginAccess() throws IOException, ServletException, ParseException {
    Assert.assertNotNull(tokenStore);
    OriginFilter filter = new OriginFilter();
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();

    MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    FilterChain mockFilterChain = Mockito.mock(FilterChain.class);

    when(tokenStore.verifyUserAgentAndHostname(any(),any(),any())).thenReturn(true);

    filter.doFilterInternal(mockRequest, mockResponse, mockFilterChain);
    filter.destroy();

}
}

What am I doing wrong here?

You create an OriginFilter object using the new operator, and since this is not done by spring, CustomJdbcTokenStore should be injected into OriginFilter manually:

@RunWith(MockitoJUnitRunner.class)
public class OFilterTest {

@InjectMocks
OriginFilter filter;

@Mock
CustomJdbcTokenStore tokenStore;


@Test
public void 
CheckIfFilterPreventsDifferentOriginAccess() throws IOException, ServletException, ParseException {
    Assert.assertNotNull(tokenStore);
    
    MockHttpServletRequest mockRequest = new MockHttpServletRequest();

    MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    FilterChain mockFilterChain = Mockito.mock(FilterChain.class);

    when(tokenStore.verifyUserAgentAndHostname(any(),any(),any())).thenReturn(true);

    filter.doFilterInternal(mockRequest, mockResponse, mockFilterChain);
    filter.destroy();

}

Also you don't need MockitoAnnotations.initMocks(this) since you use @RunWith(MockitoJUnitRunner.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM