Steve's a D
Steve's a D

Reputation: 3821

Android Studio cannot find JUnit package

I just can't seem to get a test file to find Junit4. I've been trying forever and I'm just frustrated. It says it can't find symbol junit under org. Everything I've googled (for days), and even the android docs say this is how to do it. What am I doing wrong?

Here is my test class

import org.junit.Test;
import android.app.Application;



public class ApplicationTest {
  //  @Test
    public ApplicationTest() {


    }
}

Here is my build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
   //buildToolsVersion "21.1.2"
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "stuff.morestuffs"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile files('libs/gson-2.3.1.jar')
    testCompile 'junit:junit:4.12'
}

heres my project build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

I'm using Android Studio 1.4

Upvotes: 3

Views: 6742

Answers (2)

Dmytro Melnychuk
Dmytro Melnychuk

Reputation: 2504

I solved this problem when I added following dependency to build.gradle:

androidTestCompile 'junit:junit:4.12'

If you have to test more than some separate methods and you want to test your Activity or Fragments you have to add it.

Upvotes: 1

Steve's a D
Steve's a D

Reputation: 3821

  1. For unit tests (JVM tests) the root source directory should be test, not androidTest as for Android instrument tests.

  2. Under "Build Variants", I had "Android Instrument Tests" selected. When I changed this to "Unit Tests", Android Studio correctly recognized my file.

Upvotes: 3

Related Questions