android - 使用 MockWebServer 对 RoboSpice 请求进行单元测试

标签 android unit-testing mockito robospice mockwebserver

我有一个 SpringAndroidSpiceRequest我想使用 MockWebServer 进行单元测试的子类.但是我在单元测试期间收到 java.lang.NoClassDefFoundError。我做错了什么。

SpringAndroidSpiceRequest 子类进行测试:

public class ActiveMonitorRequest extends SpringAndroidSpiceRequest<MyResponse>{
    ...        

    @Override
    public MyResponse loadDataFromNetwork() throws Exception {  
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Cache-Control", "no-cache");

        HttpEntity<MyBody> request = new HttpEntity<>(body, headers);
        RestTemplate mRestTemplate = getRestTemplate();
        ResponseEntity<MyResponse> response = mRestTemplate.exchange(url,
        HttpMethod.POST, request, MyResponse.class);

        return response.getBody();
}

单元测试方法:

@Test
public void testLoadDataFromNetwork() throws Exception {
    MockWebServer server = new MockWebServer();
    server.enqueue(new MockResponse().setBody("{}"));
    server.start();
    HttpUrl baseUrl = server.url("/helloworld");

    JacksonSpringAndroidSpiceService mService = new JacksonSpringAndroidSpiceService();
    MySpringAndroidSpiceRequest mRequest = new MySpringAndroidSpiceRequest(baseUrl);
    mRequest.setRestTemplate(mService.createRestTemplate());

    MyResponse mResponse = mRequest.loadDataFromNetwork();

    assertThat(mResponse.toString(), is(equalTo("{}")));
}

堆栈跟踪:

java.lang.NoClassDefFoundError: org/apache/http/conn/params/ConnPerRoute
    at org.springframework.http.client.support.HttpAccessor.<init>(HttpAccessor.java:55)
    at org.springframework.http.client.support.InterceptingHttpAccessor.<init>(InterceptingHttpAccessor.java:35)
    at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:169)
    at org.springframework.web.client.RestTemplate.<init>(RestTemplate.java:158)
    at com.octo.android.robospice.JacksonSpringAndroidSpiceService.createRestTemplate(JacksonSpringAndroidSpiceService.java:30)
    at com.nusclimb.live.crimp.common.spicerequest.ActiveMonitorRequestTest.testLoadDataFromNetwork(ActiveMonitorRequestTest.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.params.ConnPerRoute
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 34 more

最佳答案

看看this并添加到您的 gradle 文件中:

testCompile 'org.apache.httpcomponents:httpclient:4.5.2'

关于android - 使用 MockWebServer 对 RoboSpice 请求进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35044934/

相关文章:

android - 应用程序在启动时意外停止 - Eclipse 中没有编译错误

php - 如何在 phpunit 中断言 Yii 日志消息

unit-testing - 在 PHPUnit 中运行特定测试套件时,如何跳过运行缓慢的测试,但在需要完整代码覆盖率时仍然运行所有测试?

java - 如何在不初始化类的情况下模拟内部成员

java - 创建一个类似 "Notification Bar"的菜单

java - 如何更改 AlertDialog 消息的颜色?

java - Android 上是 AES128 还是 AES256?

java - 是否可以使用 Spock stub 或模拟 SocketChannel?

java - 在 Kotlin 中打开 getter 以将其与 Mockito 一起使用 - MissingMethodInvocationException

java - 来自父类(super class)方法调用的 Mockito NullPointerException