1

我有一个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
4

1 回答 1

1

看看这个并添加到你的 gradle 文件中:

testCompile 'org.apache.httpcomponents:httpclient:4.5.2'
于 2016-05-30T08:01:57.830 回答