Android測試教程9--聊聊配置測試環境的一些問題

最近學測試的時候,遇到一些配置上的問題,在這裏都寫下來html

testCompile , androidTestCompile 與 compile

寫測試的時候遇到這兩個配置,一時間沒明白,查了下,是這樣的java

testCompile 'org.mockito:mockito-all:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker:1.1'
compile 'com.google.dexmaker:dexmaker-mockito:1.1'
  • 1
  • 2
  • 3

testCompile這個配置項是用來給咱們的單元測試的,對應於目錄的src/test 
androidTestCompile 這個是用來給咱們測試api的,對應於目錄是src/androidTestandroid

這二者的主要區別是: 
前者是容許在通常的Java JVM的,能夠作脫離設備的測試 
後者是運行在咱們的安卓設備或者虛擬機上的狀況git

這裏寫圖片描述

另外還有一些的編譯配置

這裏寫圖片描述

Compile 
這個最多見,再github看到的那些都是這樣的形式 
compile是對全部的build type以及favlors都會參與編譯而且打包到最終的apk文件中。github

Provided 
Provided是對全部的build type以及favlors只在編譯時使用,相似eclipse中的external-libs,只參與編譯,不打包到最終apk。api

APK 
只會打包到apk文件中,而不參與編譯,因此不能再代碼中直接調用jar中的類或方法,不然在編譯時會報錯app

Test compile 
Test compile 僅僅是針對單元測試代碼的編譯編譯以及最終打包測試apk時有效,而對正常的debug或者release apk包不起做用。eclipse

Debug compile 
Debug compile 僅僅針對debug模式的編譯和最終的debug apk打包。ide

Release compile 
Release compile 僅僅針對Release 模式的編譯和最終的Release apk打包。post

===============================================

mockito

在使用這個來作mock的時候,遇到一個更加難受的問題 
若是編譯依賴只有這個的話

androidTestCompile 'org.mockito:mockito-all:1.10.19'
  • 1
  • 2

會遇到下面這個問題

java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils
at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)
at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)
at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57)
at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)
at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
at org.mockito.Mockito.mock(Mockito.java:1285)
at org.mockito.Mockito.mock(Mockito.java:1163)
at com.example.sanjay.myapplication.activity.MainActivityTest.setUp(MainActivityTest.java:56)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

stackoverflow說是要加多這個

androidTestCompile 'com.google.dexmaker:dexmaker:1.1'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1'
  • 1
  • 2
  • 3

實際加了以後,仍是不行,編譯都過,呵呵呵呵。

Error:Execution failed for task ':app:transformClassesWithDexForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: 
com.android.ide.common.process.ProcessException: 
org.gradle.process.internal.ExecException: Process 'command 'C:\Program 
Files\Java\jdk1.8.0_60_64bit\bin\java.exe'' finished with non-zero exit value 2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

索性只是保留這兩個,又有下面的問題

java.lang.IllegalArgumentException: dexcache == null
 (and no default could be found; consider setting the
  'dexmaker.dexcache' system property)
  • 1
  • 2
  • 3
  • 4

真的給跪了。

又是StackOverFlow的一個答案,在SetUp()裏面加多這個。

System.setProperty(
    "dexmaker.dexcache",
    getInstrumentation().getTargetContext().getCacheDir().getPath());
  • 1
  • 2
  • 3
  • 4

成功經過 
成功經過 
成功經過 
成功經過

參考: 
Confused about testCompile and androidTestCompile in android gradle 
Android Studio中有六種依賴

相關文章
相關標籤/搜索