- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法在此处复制确切的代码,但我将放置一个示例类来解释我面临的问题。
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
实例未注入(inject)并且为 null。
任何人都可以帮我解决我所缺少的东西吗?
我什至在@Before
方法中使用了以下内容,但仍然没有解决。
MockitoAnnotations.initMocks(this);
最佳答案
我可以验证它工作正常。请参阅下面的示例,其中我模拟了 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());
}
}
}
关于java - 如何使用 Mockito @InjectMocks 将 HttpServletRequest 注入(inject) ContainerRequestFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51720864/
在使用 JUnit 进行单元测试时,我在传递依赖项时遇到了一些麻烦。 考虑这些代码: 这是我要测试的类的依赖注入(inject),我们称之为 Controller 。 @Inject private
我有这段代码 - @Component public class ServiceA { @Autowired private ServiceB serviceB; public
这是我要模拟的类(class)。 private MemcachedClient memcachedClient; private CachedObjectFactory cachedObjectFa
我们目前正在使用 JUnit 和 Mockito 来测试我们的代码。 我们有一个如下所示的服务接口(interface)。 public interface ServiceA { void
我已经阅读了很多关于@Mock 和@InjectMocks 的讨论,但仍然找不到使用@InjectMocks 的合适或必要的情况。事实上,我不确定当我们使用@InjectMocks 时会发生什么。 考
下面的方法等价于什么: @Mock MyType1 myType1; @Autowired @InjectMocks MyType2 myType2; 我可以将 @Mock 替换为 mock(MyTy
我有一个要测试的 RestController: import javax.validation.Valid; import org.springframework.beans.factory.ann
我创建了一个包装器,用于通过 log4j 进行日志记录,扩展 HandlerInterceptorAdapter 并保持构建类型 maven 项目。我在我的 Spring Boot 应用程序中使用这个
这是我第一次使用 Mockito 进行 junit 测试。我面临 @InjectMocks 中使用的服务的 NPE 问题。我查看了其他解决方案,但即使在遵循它们之后,它也显示相同。这是我的代码。 @R
假设我有一个类: public class Boy { @Inject @Named("birthDay"
考虑下面的代码片段: @ExtendWith(MockitoExtension.class) public class MyTest { @InjectMocks ClassA cA;
我有这样的东西 @Component public class TestController { @Autowired private TestService testService;
我正在为我的单元测试项目(Spring boot rest controller)创建基础,我在传递@InjectMocks 值时遇到问题,因为它仅在@Before 中评估,因此当我尝试访问它时会抛出
我想在一种方法中使用模拟 eventService,在另一种方法中使用真实的 eventService。 @Mock(name = "eventService") private
是的,我知道,关于 Mockito 的 @InjectMocks 已经写了很多,但是仍然有一个问题我无法解决...... 假设我们有一个有四名成员的类(class)... class A {
这到底是如何运作的?按照我的理解,不应该有。LDAPGroupAccessor 正在类中进行 new 初始化,或者可以在构造函数本身中进行 new 初始化,它没有被注入(inject),不是构造函数参
我正在 Spring Mvc 上使用 Mockito 进行 JUnit 测试。测试使用 @InjectMock 和 @Mock 以及when(method(..)).thenReturn(X)。问题是
我正在测试我的类中的两个私有(private)字段,这两个字段在构造函数中初始化。 现在,当我尝试使用带有 @InjectMocks 注释的类进行调用时,它会抛出异常: Cannot instant
我有以下类(class): public class DaoService { private Dao dao; private Map map; public DaoService
为了了解 Mockito 注释工作原理的基础知识,我浏览了一些博客。 但是我对何时手动实例化用 @InjectMocks 注释的字段存在疑问,即 @InjectMocks A a = new A();
我是一名优秀的程序员,十分优秀!