Question

I have setup unit test using robolectric and gradle-android-test-plugin. I can run test without problem, but I try to use JSONOBJECT it fails with "java.lang.RuntimeException: Stub! at org.json.JSONObject.(JSONObject.java:7)" error.

Here my build.gradle file

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.+'
}

My java code:

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");
    }

}

Edit: Found the mistake!

Instead of instantiating the JSONObject in @BeforeClass, instantiated it in @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");
    }

}

The class path is not set for @BeforeClass block

Was it helpful?

Solution

Using @BeforeClass annotation in a method makes this method only access static methods and variables. To make your first code you must make obj static. That's why you're getting errors and changing the annotation to @Before works fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top