首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Mockito @InjectMock将HttpServletRequest注入ContainerRequestFilter

如何使用Mockito @InjectMock将HttpServletRequest注入ContainerRequestFilter
EN

Stack Overflow用户
提问于 2018-08-07 15:10:23
回答 1查看 1.8K关注 0票数 1

我不能在这里复制确切的代码,但我会放一个样例类来解释我面临的问题。

public XYZ implements ContainerRequestFilter{

    @Context
    HttpServletRequest httpServletRequest;

    @Override
    public void filter(ContainerRequestContext abc){
        //rest of the code below where httpServletRequest is used inside 
    }
}

因此,当我使用@InjectMocks编写测试代码时,HttpServletRequest实例不会被注入,并且为空。

有人能帮我看看我错过了什么吗。

我甚至在@Before方法中使用了下面的方法,但仍然没有解决问题。

MockitoAnnotations.initMocks(this);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-07 15:53:15

我可以验证它是否工作正常。参见下面的示例,在该示例中,我模拟HttpServletRequest并在调用getRemoteAddr()时提供远程地址。我使用这个模拟的值在ContainerRequestContext中设置一个属性。然后,我使用ArgumentCaptor捕获要测试的值。

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Context;

import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import static org.assertj.core.api.Assertions.assertThat;

public class MockitoInjectMocksTest {

    @Mock
    private HttpServletRequest request;

    @Mock
    private ContainerRequestContext requestContext;

    /**
     * With the @InjectMocks annotation, Mockito will
     * inject the filter with the mock HttpServletRequest
     */
    @InjectMocks
    private Filter filter = new Filter();

    @Before
    public void setUp() {
        // Handle all the mock creation and injection.
        MockitoAnnotations.initMocks(this);

        // Mock the HttpServletRequest#getRemoteAddr()
        // method to return a dummy IP address.
        Mockito.when(request.getRemoteAddr())
                .thenReturn("122.21.32.233");
    }

    /**
     * See the `Filter` class below. The `filter()` method doesn't
     * do much. It just grabs the remote IP address from the
     * `HttpServletRequest` and uses it to set a property on
     * the `ContainerRequestContext`. This test asserts that
     * the arguments passed to the `setProperty()` method
     * method are the correct arguments. We do that with the
     * help of Mockito's `ArgumentCaptor`,
     */
    @Test
    public void testIpPropertySet() throws Exception {
        // Call the `filter()` method that we are testing,
        // passing in the mock `ContainerRequestContext`.
        // We use a mock so that we can later verify methods
        // on it are called
        filter.filter(requestContext);

        // We create argument captors to capture the args of call to
        // `ContainerRequestContext#setProperty(String, String)`
        ArgumentCaptor<String> propNameArg = ArgumentCaptor.forClass(String.class);
        ArgumentCaptor<String> propValArg = ArgumentCaptor.forClass(String.class);

        // Verify the `ContainerRequestContext#setProperty()`
        // is called. We use the `ArgumentCaptors` to capture
        // the arguments that are passed when `setProperty()`
        // is called.
        Mockito.verify(requestContext)
               .setProperty(propNameArg.capture(), propValArg.capture());

        // Test that the arguments passed in the call to
        // `ContainerRequestContext#setProperty()` are correct.
        assertThat(propNameArg.getValue()).isEqualTo("RemoteAddress");
        assertThat(propValArg.getValue()).isEqualTo("122.21.32.233");
    }

    public static class Filter implements ContainerRequestFilter {

        @Context
        private HttpServletRequest request;

        @Override
        public void filter(ContainerRequestContext requestContext) throws IOException {
            System.out.println(request.getRemoteAddr());
            requestContext.setProperty("RemoteAddress", request.getRemoteAddr());
        }
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51720864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档