Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Class not found" when running JUnit tests from IntelliJ IDEA (Android)

I have a problem when trying to run some Android JUnit tests inside IntelliJ Idea.

My project is an Android Library project using Gradle. When I run my tests, IntelliJ complains with the following error:

Class not found: "com.domain.app.ClassTest"

But ClassTest is present inside the test package.

Here's my build.gradle:

apply plugin: 'android-library'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.10.+'
    }
}

dependencies {
    repositories {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }

    compile 'com.android.support:support-v4:19.1.+'

    compile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
}

android {
    compileSdkVersion 18
    buildToolsVersion "19.0.3"

    defaultConfig {
        versionName "1.0"
        versionCode 1

        targetSdkVersion 18
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            res.srcDirs = ['res']
        }

        androidTest {
            java.srcDirs = ['src/test/java']
        }
    }

    lintOptions {
        abortOnError false
    }
}

My project structure:

src
|_ main
   |_ java
     |_ com.domain.app
|_ test
   |_ java
      |_ com.domain.app

I'm using IntelliJ IDEA 13.1.1.

Thanks.

like image 879
Juan Herrero Diaz Avatar asked Jun 13 '14 13:06

Juan Herrero Diaz


People also ask

How do I automatically create a test class in IntelliJ?

Right-click the test root folder or package in the test root folder in which you want to create a new test and select New | Java Class. Name the new class and press Enter . Press Alt+Insert and select Test Method to generate a new test method for this class. Name the new method and press Enter .


1 Answers

Go to Project Structure -> Modules -> your_module -> Paths.

The value for 'Output path' should be filled in, but 'Test output path' will not be. Copy the text that's in 'Output path', paste into 'Test output path', but change the final 'build/intermediates/classes/debug' to 'build/test-classes'. This is because the gradle android test plugin currently dumps all compiled test output (for all variants) into the same directory. This means that currently variants are not fully supported.

Source.

like image 131
Eugene Avatar answered Sep 22 '22 15:09

Eugene