如何使用Espresso + JMockit [英] How to use Espresso + JMockit
问题描述
我想使用浓缩咖啡和但是我不进行测试. 如果您有解决办法,请帮助我.
But I don't run test. If you have resolve way, help me.
我写了一些文件(build.gradle(应用程序,项目),测试Java),如下所示.
I wrote some file(build.gradle(app, project), Test java) as follows.
build.gradle(app)
build.gradle(app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "burning.tutorial"
minSdkVersion 21
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'
// JMockit
androidTestCompile 'org.jmockit:jmockit:1.18'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test:runner:0.3'
}
build.gradle(项目)
build.gradle(Project)
projects/modules.
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()
}
}
MainActivityTest.java
MainActivityTest.java
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void sampleTest() throws Exception {
onView(withId(R.id.age)).check(matches(ViewMatchers.isDisplayed()));
}
}
此测试运行时,发生以下错误.
When this test run, happen error as follows.
:app:preDexDebugAndroidTest UP-TO-DATE
:app:dexDebugAndroidTest
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lorg/junit/runner/Runner;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
Error:Execution failed for task ':app:dexDebugAndroidTest'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_72.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
Information:BUILD FAILED
推荐答案
简短答案:
您无法轻易解决此问题,因为JMockit
的JUnit
依赖项存在错误.要修复它,您需要先修复JMockit
.
you cannot easily fix this, because JMockit
has a faulty JUnit
dependency. To fix it, you need to fix JMockit
first.
详细答案:
您的应用包含两次org.junit.runner.Runner
:
- 通过
JMockit
,因为它在库中包含Runner类文件(这很糟糕,因为它仅包含Runner类,而不包括整个库). - 通过
com.android.support.test:runner
,因为它的依赖项之一是junit
库(这是添加JUnit
支持的正确方法.
- through
JMockit
because it includes the Runner class file in the library (which is bad because it only includes the Runner class and not the whole library). - through
com.android.support.test:runner
because one of its dependencies is thejunit
library (which is the proper way to addJUnit
support.
通常的解决方法是排除以下之一:
A normal fix would be to exclude one of the following:
- 排除
JMockit
的JUnit
依赖项: 这不起作用,因为JUnit
没有定义为适当的依赖项.JUnit
仅粘贴到其自己项目中的类文件中.这是一件坏事. - 排除
com.android.support.test:runner
的JUnit
依赖项: 我们可以做到这一点,但这会从junit库中切出所有其他类,这些类没有放在JMockit
项目中.这将有效地破坏您使用的任何其他junit代码.
- exclude the
JUnit
dependency forJMockit
: This doesn't work, becauseJUnit
is not defined as a proper dependency.JUnit
merely pasted in the class file in its own project. This is a bad thing to do. - exclude the
JUnit
dependency forcom.android.support.test:runner
: We can do this, but this will cut out all the other classes from the junit library thatJMockit
did not put in their project. This will effectively break any other junit code you use.
如果您确实需要使用JMockit
,则可以考虑编辑项目,以便它正确包含junit
依赖项,然后重新编译并以这种方式将其添加到您的项目中.
If you really NEED to use JMockit
then you could consider to edit the project so it includes the junit
dependency properly and then recompile it and add it to your project this way.
这显示了错误的依赖性:
This shows the faulty dependency:
您可以看到Runner
类是如何粘贴到项目中的.
You can see how the Runner
class is just pasted into the project.
修复JMockit
依赖关系后,您需要按以下方式添加JMockit
:
After fixing the JMockit
dependencies you need to add JMockit
as follows:
androidTestCompile ('org.jmockit:jmockit:1.18') {
exclude group: 'junit'
}
这篇关于如何使用Espresso + JMockit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!