如何使用Mockito @InjectMocks将HttpServletRequest注入ContainerRequestFilter [英] How to inject HttpServletRequest into ContainerRequestFilter with Mockito @InjectMocks
问题描述
我无法在此处复制确切的代码,但是我将放置一个示例类来解释我所面临的问题.
I can't copy the exact code here but I will put a sample class that explains the problem I am facing.
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
实例,并且该实例为null.
So when I write a write test code using @InjectMocks
, the HttpServletRequest
instance is not injected and is null.
任何人都可以在这里帮助我我所想念的东西.
Can anyone help me here what I am misssing.
我甚至在@Before
方法中使用了以下内容,但仍然没有解决方法.
I have even used the following in @Before
method but still no resolution.
MockitoAnnotations.initMocks(this);
推荐答案
我可以验证它是否可以正常工作.参见下面的示例,其中我模拟了HttpServletRequest
并在调用getRemoteAddr()
时提供了一个远程地址.我使用该模拟值在ContainerRequestContext
中设置属性.然后,我使用ArgumentCaptor
s捕获要测试的值.
I can verify that it works fine. See below example, where I mock the HttpServletRequest
and provide a remote address when getRemoteAddr()
is called. I use that mocked value to set a property in the ContainerRequestContext
. I then use ArgumentCaptor
s to capture the value to test.
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());
}
}
}
这篇关于如何使用Mockito @InjectMocks将HttpServletRequest注入ContainerRequestFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!