【问题标题】:RuntimeException: Stub! on JSONObject with Robolectric运行时异常:存根!在带有 Robolectric 的 JSONObject 上
【发布时间】:2014-01-20 14:12:40
【问题描述】:

我已经使用 robolectric 和 gradle-android-test-plugin 设置了单元测试。我可以毫无问题地运行测试,但我尝试使用 JSONOBJECT 它失败并出现“java.lang.RuntimeException: Stub! at org.json.JSONObject.(JSONObject.java:7)”错误。

这是我的 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.+'
}

我的java代码:

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,而是在@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");
    }

}

@BeforeClass 块没有设置类路径

【问题讨论】:

    标签: android android-studio robolectric


    【解决方案1】:

    在方法中使用@BeforeClass 注解使该方法只能访问静态方法和变量。要制作您的第一个代码,您必须将 obj 设为静态。这就是为什么您会遇到错误并将注释更改为 @Before 工作正常。

    【讨论】:

    • 学到了新东西!谢谢!
    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 2014-03-27
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-02-27
    • 2015-06-11
    相关资源
    最近更新 更多