Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio simple Unit Test not working

I'm trying to do a simple unit test in AndroidStudio and it is not working. I have tried to follow a lot of tutorials but no sucess until now.

When I run the test, this error message is showed:

java.lang.RuntimeException: Method setUp in android.test.ActivityInstrumentationTestCase2 not mocked.

My test class:

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {

    public MainActivity activity;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        activity = getActivity();
    }

    @Test
    public void testApp() throws Exception {
        assertTrue(true);
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
        activity.finish();
    }
}

MainActivity.class:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

build.gradle(app):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.helloworld.tripbudget"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.+"
    compile files('libs/ormlite-android-4.49-SNAPSHOT.jar')
    compile files('libs/ormlite-core-4.49-SNAPSHOT.jar')
}
like image 662
Wanderson Diego Avatar asked Mar 17 '15 13:03

Wanderson Diego


1 Answers

OP found the answer himself:

Add the code below to build.gradle:

android {
    // ...
    testOptions { 
        unitTests.returnDefaultValues = true
    }
}

Reference

like image 113
Tim Avatar answered Nov 06 '22 15:11

Tim