2010-08-01 3 views
4

나는 내 발가락을 안드로이드 개발에 집중시키고있다. RESTful 리소스와 인터페이스 할 프로젝트가 있고 HTTP를 통해 params를 사용하여 기본 GET을 수행하는 방법을 파악하려고합니다. 내가 읽은 모든 것에서, 합의는 HttpURLConnection보다 HTTPClient를 선호하는 것으로 보인다.Apache DefaultHttpClient 호출의 결과는 "java.lang.RuntimeException : Stub!"입니다.

내가 키 객체를 인스턴스화을 담당하는 방법으로 래퍼 클래스를 작성했습니다

는 HttpClient를 사용하여 요청을 만들려면 :

public String get() { 
    String responseString = null; 

    HttpClient client = new DefaultHttpClient(); 
    HttpGet get = new HttpGet(); 
    try { 
     get.setURI(new URI(this.baseURL())); 
    } catch (URISyntaxException e1) { 
     e1.printStackTrace(); 
    } 

    HttpResponse response; 

    try { 
     response = client.execute(get); 
     responseString = readResponse(response.getEntity()); 

     if(response != null) { 
      System.out.println(responseString); 
     } 
    } catch(ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch(IOException e) { 
     e.printStackTrace(); 
    } 

    return responseString; 

을}

HttpClient client = new DefaultHttpClient();는 다음과 같은 예외를 throw 라인 :

java.lang.RuntimeException: Stub! 
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:5) 
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:7) 
at org.rcindustries.appmap.RestClient.get(RestClient.java:54) 
at org.rcindustries.appmap.test.functional.RestClientTest.shouldReturnSomeJSon(RestClientTest.java:26) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

HttpClient에서 본 모든 예에서는 GET 및 POST와 유사한 구조를 사용합니다. 아파치 커먼즈 라이브러리는 안드로이드 SDK와 번들로 제공되는 것과 상당히 다른 표준 라이브러리입니까?

답변

6

나는 이것이 당신이 그 플랫폼에서 그 단위 테스트를 실행할 수 없다는 것을 말하는 안드로이드의 방법이라고 생각합니다. Android 플랫폼과 상호 작용하는 유닛 테스트 (예 :이 경우 네트워크)는 실제 Android 기기 또는 작동중인 Android 에뮬레이터에서 실행해야합니다.

분명히 Android SDK의 스텁 클래스에 대한 단위 테스트를 실행하고 있습니다.

+0

그래서 메소드를 스텁 아웃 한 서비스/라이브러리가 있습니까? dev.android.com에 이것에 대해 설명하는 내용이 있습니까? 저는 Android 생태계에 익숙하지 않아이 부분을 찾을 곳이 확실하지 않습니다. – manlycode

+0

예외 메시지를 "검색"하고 스택 추적에서 추론을 그려서이 사실을 알았습니다. 나는 안드로이드 개발을 직접하지 않는다. –

+1

일반적으로 Java에서 스텁 메서드 만있는 인터페이스를 노출하는 것이 일반적인 방법입니까? 대학 시절에 사용 했으므로 오래 동안이었습니다. 자바로 TDD를 처음 접했습니다. – manlycode

8

관심이있는 분이라면 HttpClient 대신 DateUtils의 스텁 오류가 발생 했는데도 비슷한 문제가 발생했습니다.

스티븐 절대적으로 올바른 것으로 보인다 - 안드로이드 플랫폼의 일부인 클래스 에뮬레이터를 필요로하고 실행 : http://simpleprogrammer.com/2010/07/27/the-best-way-to-unit-test-in-android/

위의 링크의 정말 간단히 요약 :

사실로 , 모든 메소드는 호출 할 때 "스텁!"메시지와 함께 예외를 던지도록 스텁 아웃됩니다. 얼마나 귀여워.

실제 android.jar 구현은 에뮬레이터 또는 실제 Android 기기에서 실행됩니다.

따라서, 당신이 할 수있는 단위 테스트 ... 에뮬레이터에

  • - 전체 안드로이드 플랫폼, "진짜"안드로이드 환경 등을 얻을 수 (그러나 일반적인 자바 단위 테스트 도구의 힘없는 - 예를 들어, JMock)

또는 JVM을 가진

  • - 빠른 등 JMock 같은 단위 테스트 도우미를 사용할 수 있습니다,하지만 당신은 테스트 할 수 없습니다 Android 플랫폼에 의존하는 모든 것.
+0

android.jar SDK 라이브러리의 모든 메소드는 스텁 메소드 인 것으로 보입니다. – HackNone

0

이 스레드 종류는 오래되었지만 모르는 사람에게는 Robolectric이 테스트를 위해이 문제를 해결합니다.

1

Android SDK 23에 Proguard 및 com.apache.http.legacy 라이브러리를 사용하면 이런 현상이 발생합니다.제대로 응용 프로그램으로 컴파일 된 스텁을 오버라이드 (override) 할 수있는 시스템 아파치 구현을 할 수

-keep class org.apache.http.** { *; } 
-keep class org.apache.commons.codec.** { *; } 
-keep class org.apache.commons.logging.** { *; } 
-keep class android.net.compatibility.** { *; } 
-keep class android.net.http.** { *; } 
-keep class com.android.internal.http.multipart.** { *; } 
-dontwarn org.apache.http.** 
-dontwarn android.webkit.** 

을 : 내 Proguard와의 설정이 추가 후

그것은했다.

관련 문제