Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android unit test not mocked

I followed this guide https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support but i am stuck with this error:

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.

I modified by Gradle build like the guide says but it doesn't make a difference

 testOptions { 
    unitTests.returnDefaultValues = true
  }
like image 459
user1634451 Avatar asked Apr 01 '15 22:04

user1634451


People also ask

Is mocking unit tested?

What is mocking? Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

What is mock test in Android?

Android App Development for BeginnersEvery mock test is supplied with a mock test key to let you verify the final score and grade yourself.

What is AndroidX test?

AndroidX Test is a collection of Jetpack libraries that lets you run tests against Android apps. It also provides a series of tools to help you write these tests. For example, AndroidX Test provides JUnit4 rules to start activities and interact with them in JUnit4 tests.

What should I test in unit tests Android?

Unit tests or small tests only verify a very small portion of the app, such as a method or class. End-to-end tests or big tests verify larger parts of the app at the same time, such as a whole screen or user flow. Medium tests are in between and check the integration between two or more units.


3 Answers

JSON is bundled up with the Android SDK, so you'll just be hitting a stub. You can pull in a JSON jar, which will provide real objects to use.

To do this, you'll need to add this to your build.gradle:

testImplementation 'org.json:json:20140107'

Alternatively, you can download and include the jar.

testCompile files('libs/json.jar')

Note that the latest version of JSON is built for Java 8, so you'll need to grab 20140107 You may also need to clean and rebuild the project.

like image 147
Ben Pearson Avatar answered Oct 25 '22 23:10

Ben Pearson


I think you trying to run tests with org.json.JSONObject which is part of Android Framework on pure jUnit.

From 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.

You need to emulate Android environment you can use for this purpose Robolectric or InstrumentationTests

like image 40
ar-g Avatar answered Oct 26 '22 00:10

ar-g


You need to add the following to the build.gradle:

android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}

and

dependencies {
//...
    testImplementation 'org.json:json:20180813'
}
like image 8
Thiago Avatar answered Oct 25 '22 23:10

Thiago