15

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.

Juan Herrero Diaz
  • 833
  • 1
  • 7
  • 17
  • Possible duplicate of [Running tests on Intellij: Class not found](http://stackoverflow.com/questions/22105264/running-tests-on-intellij-class-not-found) – Victor Sergienko Nov 19 '15 at 11:12

5 Answers5

2

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.

Eugene
  • 59,186
  • 91
  • 226
  • 333
1

Old question, but just ran into this problem and found that the answer is to store JUnit test files at the following path

module-name/src/test/java/

If the directory doesn't exist yet, create it and put your test files in there.

Source: https://developer.android.com/training/testing/unit-testing/local-unit-tests

Gordon M
  • 11
  • 1
0

Change the testing library to jUnit4.

Maybe when creating the testing class you choose different testing library instead of jUnit

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Nabase1
  • 1
  • 1
0

Path with problem character

This is unrelated to the actual question but searchers might come here and it might help. My problem was a class not found error, using a JUnit test ¯_(ツ)_/¯

I'd created a path including directory representing spanned year (2020/2021) in finder and it substituted the forward-slash for a colon (/ → :)

Replacing the colon with an underscore fixed the issue.

Peter Shannon
  • 191
  • 1
  • 3
-10

Try to rename "test" to "androidTest"

src |_ main |_ java |_ com.domain.app |_ **androidTest** |_ java |_ com.domain.app

http://tools.android.com/tech-docs/new-build-system/migrating_to_09

macray
  • 1
  • 2