与安卓的Gradle整合工作室始终roboelectric给人包org.junit不存在错误 [英] Integrating roboelectric with android studio with gradle always gives package org.junit does not exist error
问题描述
我想roboelectric在我的新项目进行整合。我用以下链接了解和impliment roboelectric
I am trying to integrate roboelectric in my new project . I used following links to understand and impliment roboelectric
的http:// pivotallabs .COM /设置-UP-robolectric功能于Android的工作室-1-1-ON-OS-X /
的http:// nenick- android.blogspot.in/2015/02/android-studio-110-beta-4-and.html
http://raptordigital.blogspot.in/2014/ 02 /测试驱动开发,with.html
但我刚开上运行我的测试类以下错误
But I am gettin following error on running my test class
Error:Gradle: Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
E:\junitTest\android-studio-robolectric-example-master\JunitTestApp\app\src\test\java\com\inapp\junittestapp\SampleTest.java
Error:(7, 17) Gradle: error: package org.junit does not exist
Error:(8, 24) Gradle: error: package org.junit.runner does not exist
Error:(9, 23) Gradle: error: package org.robolectric does not exist
Error:(10, 34) Gradle: error: package org.robolectric.annotation does not exist
Error:(11, 31) Gradle: error: package org.robolectric.shadows does not exist
Error:(13, 27) Gradle: error: package org.hamcrest does not exist
Error:(13, 1) Gradle: error: static import only from classes and interfaces
Error:(14, 27) Gradle: error: package org.hamcrest does not exist
Error:(14, 1) Gradle: error: static import only from classes and interfaces
Error:(15, 24) Gradle: error: package org.junit does not exist
Error:(15, 1) Gradle: error: static import only from classes and interfaces
Error:(16, 30) Gradle: error: package org.robolectric does not exist
Error:(16, 1) Gradle: error: static import only from classes and interfaces
Error:(17, 30) Gradle: error: package org.robolectric does not exist
Error:(17, 1) Gradle: error: static import only from classes and interfaces
Error:(18, 30) Gradle: error: package org.robolectric does not exist
Error:(18, 1) Gradle: error: static import only from classes and interfaces
Error:(25, 2) Gradle: error: cannot find symbol class RunWith
Error:(26, 2) Gradle: error: cannot find symbol class Config
Error:(30, 6) Gradle: error: cannot find symbol class Test
Error:(32, 38) Gradle: error: cannot find symbol method setupActivity(Class<MainActivity>)
Error:(36, 9) Gradle: error: cannot find symbol method clickOn(Button)
Error:(38, 41) Gradle: error: cannot find symbol variable ShadowAlertDialog
Error:(39, 42) Gradle: error: cannot find symbol method notNullValue()
Error:(41, 9) Gradle: error: cannot find symbol class ShadowAlertDialog
Error:(41, 47) Gradle: error: cannot find symbol method shadowOf(AlertDialog)
Error:(42, 61) Gradle: error: cannot find symbol method is(String)
加我下面的测试类code
Added below my test class code
import android.app.AlertDialog;
import android.widget.Button;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowAlertDialog;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.robolectric.Robolectric.clickOn;
import static org.robolectric.Robolectric.setupActivity;
import static org.robolectric.RobolectricBase.shadowOf;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "app/src/main/AndroidManifest.xml", emulateSdk = 18)
public class SampleTest {
@Test
public void buttonTapDisplaysAnAlertDialog() {
MainActivity helloActivity = setupActivity(MainActivity.class);
Button button = (Button) helloActivity.findViewById(R.id.button);
clickOn(button);
AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
assertThat(latestAlertDialog, is(notNullValue()));
ShadowAlertDialog shadowAlertDialog = shadowOf(latestAlertDialog);
assertThat(shadowAlertDialog.getTitle().toString(), is("Hi"));
}
}
和我的应用程序文件的gradle是
and my app gradle file is
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.junittestapp"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { java.srcDirs = ['src/main/java', 'src/test/java/'] } }
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:22.1.1'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
}
我发现同样的错误,在这个博客太
i found same error in this blog too
<一个href=\"https://discuss.gradle.org/t/why-cant-my-simplest-one-line-build-gradle-compile-tests-with-junit-jar/1868\" rel=\"nofollow\">https://discuss.gradle.org/t/why-cant-my-simplest-one-line-build-gradle-compile-tests-with-junit-jar/1868
我认为我丢失的东西,我搜索了很多无法找到一个解决方案,任何帮助将AP preciable。
I think something i am missing ,i searched a lot couldnt find a solution ,any help will appreciable.
在此先感谢
推荐答案
您的问题是,你有一个 sourceSet 的宣布为包含code为应用程序的主和测试。这就是为什么摇篮任务:应用程序:compileDebugJava',因为它不具备 的junit.jar 的在那一刻(它在注射 testCompile 失败EM> - 时间)。声明两个不同的sourceSet的,一切都应该工作。
Your problem is that you have one sourceSet declared as main that contains code for application and for test. That's why Gradle task ':app:compileDebugJava' fails because it doesn't have junit.jar at that moment (it's injected at testCompile - time). Declare two different sourceSet's and everything should work.
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
robolectric {
java.srcDir file('src/test/java/')
resources.srcDir file('src/test/resources')
}
}
这篇关于与安卓的Gradle整合工作室始终roboelectric给人包org.junit不存在错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!