2014-01-20 2 views
1

robolectric 및 gradle-android-test-plugin을 사용하여 설정 단위 테스트를했습니다. 문제없이 테스트를 실행할 수 있지만 JSONOBJECT를 사용하려고합니다. "java.lang.RuntimeException : Stub! org.json.JSONObject. (JSONObject.java:7)"오류로 실패합니다. 여기RuntimeException : 스텁! Robolectric과 JSONObject에

내 build.gradle 파일

buildscript { 
    repositories { 
     mavenCentral() 
     maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } 
    } 

    dependencies { 
     classpath 'com.android.tools.build:gradle:0.7.+' 
     classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT' 
    } 
} 

apply plugin: 'android' 
apply plugin: 'android-test' 

repositories { 
    mavenCentral() 
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } 
} 

android { 
    compileSdkVersion 19 
    buildToolsVersion '19.0.0' 

    defaultConfig { 
     minSdkVersion 14 
     targetSdkVersion 19 
    } 

    buildTypes { 
     release { 
      runProguard false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 
     } 
    } 

    sourceSets { 
     instrumentTest.setRoot('src/test') 
    } 
} 

dependencies { 
    compile 'com.android.support:support-v4:+' 
    compile files('libs/org.eclipse.paho.client.mqttv3.jar') 
    compile files('libs/volley.jar') 

    testCompile 'junit:junit:4.10' 
    testCompile 'org.robolectric:robolectric:2.3-SNAPSHOT' 
    testCompile 'com.squareup:fest-android:1.0.+' 

    instrumentTestCompile 'junit:junit:4.10' 
    instrumentTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT' 
    instrumentTestCompile 'com.squareup:fest-android:1.0.+' 
} 

내 자바 코드 :

import org.json.JSONException; 
import org.json.JSONObject; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.robolectric.annotation.Config; 

import com.example.RobolectricGradleTestRunner; 

@Config(emulateSdk = 18) 
@RunWith(RobolectricGradleTestRunner.class) 
public class VisitorChatTest { 
    JSONObject obj; 

    @BeforeClass 
    public void jsonTest() throws JSONException{ 
     obj = new JSONObject("{ \"hello\" : \"test\"} "); 
    } 

    @Test 
    public void test() throws JSONException{ 
     assertEquals(obj.getString("hello"), "test"); 
    } 

} 

편집 : 실수를 발견! 대신 @BeforeClass의 된 JSONObject의 인스턴스의

는 클래스 경로가 @BeforeClass이 방법 @BeforeClass 주석을 사용

답변

0

이 방법을하게 차단 설정되지 않습니다 @Before

import org.json.JSONException; 
import org.json.JSONObject; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.robolectric.annotation.Config; 

import com.example.RobolectricGradleTestRunner; 

@Config(emulateSdk = 18) 
@RunWith(RobolectricGradleTestRunner.class) 
public class VisitorChatTest { 
    JSONObject obj; 

    @Before 
    public void jsonTest() throws JSONException{ 
     obj = new JSONObject("{ \"hello\" : \"test\"} "); 
    } 

    @Test 
    public void test() throws JSONException{ 
     assertEquals(obj.getString("hello"), "test"); 
    } 

} 

에서 인스턴스화 정적 메서드와 변수에만 액세스합니다. 첫 번째 코드를 작성하려면 obj를 정적으로 만들어야합니다. 그래서 오류가 발생하고 @Before 잘 작동하도록 주석을 변경하는 것입니다.

+0

새로운 것을 배우기! 감사! – Shankar