5

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.

User0123456789
  • 760
  • 2
  • 10
  • 25
gascon95
  • 115
  • 1
  • 10
  • Are you writing unit tests?(`/src/test`) or ui tests (`src/androidTest`)? – Jared Burrows Apr 01 '16 at 15:44
  • Yes, they are Instrumented unit tests, so in order to run them, they must be in src/androidTest, acording to this: http://developer.android.com/intl/es/training/testing/start/index.html#run-instrumented-tests – gascon95 Apr 01 '16 at 15:49
  • Look here: https://github.com/googlesamples/android-testing/tree/master/ui/espresso/BasicSample. Use Junit unit, for unit tests. – Jared Burrows Apr 01 '16 at 15:58
  • 2
    Ok, I found something on that repo, but not on that project. Either way, "look here" and a URL to a GitHub Repository is not much of an answer... Thanks, anyway. – gascon95 Apr 01 '16 at 16:14
  • "Either way, "look here" and a URL to a GitHub Repository is not much of an answer". Really? You are the one that needs help. I didn't supply an answer, this is just a comment. You should use Google examples since you are new to Android. – Jared Burrows Apr 01 '16 at 16:19
  • You are right, that wasn't kind of me, you were just trying to help and, in a way, you did it. So, srry. – gascon95 Apr 01 '16 at 17:06

1 Answers1

4

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'
gascon95
  • 115
  • 1
  • 10
  • I also had to do what this answer suggested https://stackoverflow.com/a/42678218/1274764 – Hardy Jul 15 '17 at 01:29