How to use Espresso + JMockit

I want to use Espresso and JMockito .

But I do not run the test. If you have a way to resolve, help me.

I wrote a file (build.gradle (application, project), Test java) as follows.


build.gradle (application)

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 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 (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

 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 performing this test, complete the 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 
+4
source share
1 answer

Short answer:

you cannot easily fix this because JMockit has a faulty JUnit dependency. To fix this, you first need to fix JMockit .

Long answer:

Your application contains org.junit.runner.Runner twice:

  • through JMockit because it includes the Runner class file in the library (which is bad because it includes only the Runner class and not the entire library).
  • via com.android.support.test:runner , because one of its dependencies is the JUnit library (which is the right way to add JUnit support).

A normal fix would be to exclude one of the following:

  • exclude JUnit dependency for JMockit : This does not work because JUnit not defined as a valid dependency. JUnit simply inserted into the class file in its own project. This is a bad thing.
  • exclude JUnit dependency for com.android.support.test:runner : We can do this, but it cuts out all other classes from the junit library that JMockit did not contribute to its project. This will effectively destroy any other unit code that you use.

If you really need to use JMockit , you might consider editing the project so that it correctly includes the JUnit dependency, then recompiling it and adding it to your project this way.

This shows an incorrect dependency:

Android Studio screenshot

You can see how the Runner class is simply inserted into the project.

After fixing the JMockit dependencies JMockit you need to add JMockit as follows:

 androidTestCompile ('org.jmockit:jmockit:1.18') { exclude group: 'junit' } 
+1
source

All Articles