java - mockito 和 jetty-client : receiving NullPointerException when start() and stop() methods are invoked

标签 java testing mocking jetty mockito

我正在测试这段代码

  this.httpClient.start();
  ContentResponse response = this.httpClient.newRequest(Protocol.IMAGES())
                                            .method(HttpMethod.GET)
                                            .send();

  if(response.getStatus() != 200)
    throw new HostIsNotReachableException();

  images = ImageList.fromJson(response.getContentAsString());

  this.httpClient.stop();

使用

HttpClient httpClient = mock(HttpClient.class);
Request request = mock(Request.class);
ContentResponse response = mock(ContentResponse.class);
when(httpClient.newRequest(Protocol.IMAGES())).thenReturn(request);
when(request.method(HttpMethod.GET)).thenReturn(request);
when(request.send()).thenReturn(response);
when(response.getStatus()).thenReturn(200);
when(response.getContentAsString()).thenReturn(gson.toJson(images));
...
verify(httpClient, times(1)).start();
verify(httpClient, times(1)).stop();
verify(httpClient, times(1)).newRequest(Protocol.IMAGES());
verify(request, times(1)).method(HttpMethod.GET);
verify(request, times(1)).send();

调用 start() 和/或 stop() 方法时,测试报告意外的 NullPointerException。模拟定义中有什么我想念的吗?

这是异常的日志:

java.lang.NullPointerException
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:61)
at com.meetecho.docker.client.ClientTest.itShouldGetTheImagesList(ClientTest.java:99)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

最佳答案

很好的问题,我遇到了一个非常相似的问题,这个 stackoverflow 出现在我的谷歌搜索结果的顶部。

在我的例子中,我试图测试将我的自定义过滤器添加到我的 ApplicationContext 的代码片段,并在必要时模拟对象。我遇到了一个问题,当我的代码调用 filterHolder.start()(实际上是 AbstractLifeCycle 中的 final方法 start())时,它会抛出 NullPointerException。

进一步构建您自己的 self 答案:您可以使用 Powermock 来克服 Mockito 无法模拟 final方法的问题。

在项目中包含 Powermock 库后,启用 Powermock 运行器并标记 AbstractLifeCycle.class 类以进行字节码操作:

@PrepareForTest(AbstractLifeCycle.class)
@RunWith(PowerMockRunner.class)
public class MyTestClass extends Assert {

然后,模拟 initialize() 和 start() 方法:

doAnswer(new DoesNothing()).when(holder).initialize();
doAnswer(new DoesNothing()).when(holder).start();

它应该可以正常工作。

关于java - mockito 和 jetty-client : receiving NullPointerException when start() and stop() methods are invoked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25149600/

相关文章:

python - Python 的 create_autospec 中的实例参数有什么作用?

java - pom 中的 Arquillian ShrinkWrap maven 依赖项

java - 类构造函数上的 JSP 编译错误

javascript - 使用 sinon 测试 React PropTypes

javascript - 无限 Jasmine 超时

python - 模拟用户输入()

java - 根据Java/Grails中的元素从 map 中提取/过滤数据

java - 为什么使用 Swing 绘画在 Java 8 和 Java 6 中表现不同?

python - 在单元测试中模拟 python 装饰器

ruby-on-rails - rspec 版本 2.14 的未定义方法 `rspec_reset'