Java 从 IntelliJ IDEA (Android) 运行 JUnit 测试时“找不到类”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24206315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Class not found" when running JUnit tests from IntelliJ IDEA (Android)
提问by Juan Herrero Diaz
I have a problem when trying to run some Android JUnit tests inside IntelliJ Idea.
尝试在 IntelliJ Idea 中运行一些 Android JUnit 测试时遇到问题。
My project is an Android Library project using Gradle. When I run my tests, IntelliJ complains with the following error:
我的项目是一个使用 Gradle 的 Android 库项目。当我运行我的测试时,IntelliJ 抱怨以下错误:
Class not found: "com.domain.app.ClassTest"
But ClassTest
is present inside the test package.
但是ClassTest
存在于测试包中。
Here's my build.gradle:
这是我的 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.
我正在使用 IntelliJ IDEA 13.1.1。
Thanks.
谢谢。
回答by macray
Try to rename "test" to "androidTest"
尝试将“test”重命名为“androidTest”
src
|_ main
|_ java
|_ com.domain.app
|_ **androidTest**
|_ java
|_ com.domain.app
src
|_ main
|_ java
|_ com.domain.app
|_ **androidTest**
|_ java
|_ com.domain.app
http://tools.android.com/tech-docs/new-build-system/migrating_to_09
http://tools.android.com/tech-docs/new-build-system/migrating_to_09
回答by Eugene
Go to Project Structure -> Modules -> your_module -> Paths
.
去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.
Output path
应填写“ ”的值,但不会填写“ Test output path
”。复制“ ”中的文本Output path
,粘贴到“测试输出路径”中,但将最后的“ build/intermediates/classes/debug
”更改为“ build/test-classes
”。这是因为 gradle android 测试插件当前将所有编译的测试输出(对于所有变体)转储到同一目录中。这意味着不完全支持当前的变体。
来源。
回答by Gordon M
Old question, but just ran into this problem and found that the answer is to store JUnit test files at the following path
老问题,不过刚好碰到这个问题,发现答案是将JUnit测试文件存放在如下路径
module-name/src/test/java/
模块名称/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
来源:https: //developer.android.com/training/testing/unit-testing/local-unit-tests