1

MockWebServerSpringAndroidSpiceRequestを使用して単体テストしたいサブクラスがあります。ただし、単体テスト中に取得しています。私は何を間違っていましたか。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 に答える