Java 如何使用Mockito@InjectMocks将HttpServletRequest注入ContainerRequestFilter
我不能在这里复制确切的代码,但我将提供一个示例类来解释我所面临的问题Java 如何使用Mockito@InjectMocks将HttpServletRequest注入ContainerRequestFilter,java,unit-testing,mockito,jax-rs,Java,Unit Testing,Mockito,Jax Rs,我不能在这里复制确切的代码,但我将提供一个示例类来解释我所面临的问题 public XYZ implements ContainerRequestFilter{ @Context HttpServletRequest httpServletRequest; @Override public void filter(ContainerRequestContext abc){ //rest of the code below where httpS
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
谁能帮我一下我错过了什么
我甚至在@Before
方法中使用了以下内容,但仍然没有解决方案
MockitoAnnotations.initMocks(this);
我可以证实它工作正常。请参见下面的示例,其中我模拟了
HttpServletRequest
,并在调用getRemoteAddr()
时提供了一个远程地址。我使用该模拟值在ContainerRequestContext
中设置属性。然后我使用ArgumentCaptor
s捕获要测试的值
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());
}
}
}
import java.io.IOException;
导入javax.servlet.http.HttpServletRequest;
导入javax.ws.rs.container.ContainerRequestContext;
导入javax.ws.rs.container.ContainerRequestFilter;
导入javax.ws.rs.core.Context;
导入org.junit.Before;
导入org.junit.Test;
导入org.mockito.ArgumentCaptor;
导入org.mockito.InjectMocks;
导入org.mockito.Mock;
导入org.mockito.mockito;
导入org.mockito.MockitoAnnotations;
导入静态org.assertj.core.api.Assertions.assertThat;
公共类MockitoInjectMocksTest{
@嘲弄
私有HttpServletRequest;
@嘲弄
私有ContainerRequestContext请求上下文;
/**
*使用@InjectMocks注释,Mockito将
*将模拟HttpServletRequest注入过滤器
*/
@注射模拟
私有过滤器=新过滤器();
@以前
公共作废设置(){
//处理所有的模拟创建和注入。
initMocks(this);
//模拟HttpServletRequest#getRemoteAddr()
//方法返回虚拟IP地址。
Mockito.when(request.getRemoteAddr())
。然后返回(“122.21.32.233”);
}
/**
*请参阅下面的'Filter'类,'Filter()'方法不支持
*做很多。它只是从服务器上获取远程IP地址
*`HttpServletRequest`并使用它设置
*“ContainerRequestContext”。此测试断言
*传递给`setProperty()`方法的参数
*方法是正确的参数
*借助莫基托的“ArgumentCaptor”,
*/
@试验
public void testIpPropertySet()引发异常{
//调用我们正在测试的'filter()'方法,
//传入模拟的“ContainerRequestContext”。
//我们使用mock,以便稍后验证方法
//它的名字叫
filter.filter(请求上下文);
//我们创建参数捕获器来捕获调用的参数
//`ContainerRequestContext#setProperty(字符串,字符串)`
ArgumentCaptor propNameArg=ArgumentCaptor.forClass(String.class);
ArgumentCaptor propValArg=ArgumentCaptor.forClass(String.class);
//验证“ContainerRequestContext#setProperty()`
//调用。我们使用'ArgumentCaptors'来捕获
//当`setProperty()时传递的参数`
//被称为。
验证(requestContext)
.setProperty(propNameArg.capture(),propValArg.capture());
//测试在对的调用中传递的参数
//`ContainerRequestContext#setProperty()`是正确的。
assertThat(propNameArg.getValue()).isEqualTo(“远程地址”);
资产(propValArg.getValue()).isEqualTo(“122.21.32.233”);
}
公共静态类筛选器实现ContainerRequestFilter{
@上下文
私有HttpServletRequest;
@凌驾
公共无效筛选器(ContainerRequestContext requestContext)引发IOException{
System.out.println(request.getRemoteAddr());
setProperty(“RemoteAddress”,request.getRemoteAddr());
}
}
}
我可以验证它是否工作正常。请参见下面的示例,其中我模拟了HttpServletRequest
,并在调用getRemoteAddr()
时提供了一个远程地址。我使用该模拟值在ContainerRequestContext
中设置属性。然后我使用ArgumentCaptor
s捕获要测试的值
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());
}
}
}
import java.io.IOException;
导入javax.servlet.http.HttpServletRequest;
导入javax.ws.rs.container.ContainerRequestContext;
导入javax.ws.rs.container.ContainerRequestFilter;
导入javax.ws.rs.core.Context;
导入org.junit.Before;
导入org.junit.Test;
导入org.mockito.ArgumentCaptor;
导入org.mockito.InjectMocks;
导入org.mockito.Mock;
导入org.mockito.mockito;
导入org.mockito.MockitoAnnotations;
导入静态org.assertj.core.api.Assertions.assertThat;
公共类MockitoInjectMocksTest{
@嘲弄
私有HttpServletRequest;
@嘲弄
私有ContainerRequestContext请求上下文;
/**
*使用@InjectMocks注释,Mockito将
*将模拟HttpServletRequest注入过滤器
*/
@注射模拟
私有过滤器=新过滤器();
@以前
公共作废设置(){
//处理所有的模拟创建和注入。
initMocks(this);
//模拟HttpServletRequest#getRemoteAddr()
//方法返回虚拟IP地址。
Mockito.when(request.getRemoteAddr())
。然后返回(“122.21.32.233”);
}
/**
*请参阅下面的'Filter'类,'Filter()'方法不支持
*做很多。它只是从服务器上获取远程IP地址
*`HttpServletRequest`并使用它设置
*“ContainerRequestContext”。此测试断言
*传递给`setProperty()`方法的参数
*方法是正确的参数
*借助莫基托的“ArgumentCaptor”,
*/
@试验
public void testIpPropertySet()引发异常{
/