2015-04-01 14 views
32

ho seguito questa guida https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support ma io sono bloccato con questo errore:unit test Android non deriso

junit.framework.AssertionFailedError: Exception in constructor: testSaveJson (java.lang.RuntimeException: Method put in org.json.JSONObject not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details. 

ho modificato dal Gradle costruire come la guida dice, ma non fa la differenza

testOptions { 
    unitTests.returnDefaultValues = true 
    } 
+0

Abbiamo bisogno di maggiori informazioni. Aggiungi del codice, spiega cosa stai cercando di ottenere. Unit test su un serializzatore JSON (de)? –

risposta

73

JSON è fornito in bundle con l'SDK di Android, quindi devi semplicemente essere colpire un stub. Puoi inserire un jar JSON, che fornirà oggetti reali da utilizzare.

Per fare questo, è necessario aggiungere questo alla tua build.gradle:

testCompile 'org.json:json:20140107'

In alternativa, è possibile scaricare e includere il vaso.

testCompile files('libs/json.jar') 

Si noti che l'ultima versione di JSON è costruito per Java 8, quindi avrai bisogno di afferrare 20140107

+9

Sembra che ora puoi tirare usando Gradle, quindi 'testCompile 'org.json: json: 20140107'' va bene. –

+0

Perché dici che tirare da Maven Central non funziona? –

+0

@IgorGanapolsky sembra funzionare di nuovo, quindi aggiornerò la risposta –

3

Penso che si stia tentando di eseguire test con org.json.JSONObject che fa parte di Android Framework su puro jUnit.

Da docs:

The android.jar file that is used to run unit tests does not contain any actual code - that is provided by the Android system image on real devices. Instead, all methods throw exceptions (by default).

We are aware that the default behavior is problematic when using classes like Log or TextUtils and will evaluate possible solutions in future releases.

Hai bisogno di emulare l'ambiente Android è possibile utilizzare per questo scopo Robolectric o InstrumentationTests

+0

Quindi non c'è modo di usare JSON nei normali test di Junit? –

0
android { 


testOptions { 
    unitTests.returnDefaultValues = true 
} } 

dependencies { 
testImplementation libs.leakCanaryNoOp 
testImplementation tests.jUnit 
testImplementation tests.mockito 
testImplementation(tests.mokitoKotlin) { 
    exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib" 
    exclude group: "org.jetbrains.kotlin", module: "kotlin-runtime" 
    exclude group: "org.jetbrains.kotlin", module: "kotlin-reflect" 
    exclude group: "org.mockito", module: "mockito-core" 
} 
testImplementation tests.powerMock 
testImplementation tests.powerMockApiMockito 
testImplementation (tests.robolectric) { 
    exclude group: 'org.robolectric', module: 'robolectric-resources:' 
} 
testImplementation (tests.robolectricShadowsSupport){ 
    exclude group: 'org.robolectric', module: 'robolectric' 
} 
kaptTest libs.daggerCompiler 

}

+0

Possiamo avere più spiegazioni al riguardo? – RousseauAlexandre