Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Junit tests package org.junit does not exist

Tags:

I'm trying to implement a test class in Android Studio to make some test on a DBAdapter. Since I need to run the test on my mobile phone in order to use the database, I have created an Instrumentation Unit Test (cause I've tried to do it just with a Unit test but I need to use the database and so, and those are only run locally).

The problem is that when I try to run the test class, using my mobile phone as running device, the compiler throws the following error:

error: package org.junit does not exist

I've been looking for a solutione, but I found none.

This is my test class (just the skeleton):

import org.junit.Test;
import android.support.test.runner.AndroidJUnit4;

import static org.junit.Assert.*;


public class DbAdapterTest {

    @Test
    public void testCreateSeries() throws Exception {

    }
}

And this one is the build.gradle script:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.w2w.whattowatch"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.9.5"
}

I also have another issue. As you can see, I also imported this:

import android.support.test.runner.AndroidJUnit4;

But, even before running, it says of "runner" that "cannot resolve the symbol". I've added the TestInstrumentationRunner on build.gradle, but still not working.

like image 968
gascon95 Avatar asked Apr 01 '16 15:04

gascon95


1 Answers

OK, I solve it so, this is the solution that worked for me.

I didn't have this dependences, so I add them to the build.gradle script:

androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
like image 137
gascon95 Avatar answered Oct 12 '22 01:10

gascon95