Android unit test not mocked

24,196

Solution 1

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.

Solution 2

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

Solution 3

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

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

and

dependencies {
//...
    testImplementation 'org.json:json:20180813'
}

Solution 4

Solution for the problem in if you're using Kotlin DSL:

testOptions {
    unitTests.isReturnDefaultValues = true
}

testImplementation("org.json:json:20210307")
Share:
24,196
user1634451
Author by

user1634451

Updated on October 18, 2021

Comments

  • user1634451
    user1634451 over 2 years

    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
      }
    
  • Donal Lafferty
    Donal Lafferty almost 9 years
    Seems you can now pull using Gradle, so testCompile 'org.json:json:20140107' is fine.
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    Why do you say that pulling from maven central doesn't work?
  • IgorGanapolsky
    IgorGanapolsky over 8 years
    So there is no way to use JSON in plain Junit tests?
  • Ben Pearson
    Ben Pearson over 8 years
    @IgorGanapolsky it seems to be working again, so I'll update the answer
  • Nicolas Cornette
    Nicolas Cornette about 8 years
    You should use THIS version : org.json:json:20080701, which matches the Android version. In this version, getString() returns null when value is json is null, instead of throwing an exception.
  • Ben Pearson
    Ben Pearson almost 8 years
    @NicolasCornette to which Android version are you referring?
  • Nicolas Cornette
    Nicolas Cornette almost 8 years
    @BenPearson this is true on all android versions
  • tbm
    tbm over 7 years
    Simply including this in build.gradle does not seem to import the correct libraries - I'm still getting the 'not mocked' error. What needs to be done to use the explicit library modules?
  • tbm
    tbm over 7 years
    Never mind - turns out a clean/ rebuild fixed the problem.
  • alexandre-rousseau
    alexandre-rousseau over 6 years
    Can we have more explanations about this?
  • Staszek
    Staszek over 4 years
    It works, but as @tbm write - only after clean and rebuild. Can you add it to the answer?
  • Satish Ghuge
    Satish Ghuge about 4 years
    Is it required for Test Implementation only?
  • Satish Ghuge
    Satish Ghuge about 4 years
    Latest version available is 20190722
  • Nantoka
    Nantoka over 2 years
    Using Robolectric did the job for me. Interestingly the error message appeared after we upgraded to Android Studio Arctic Fox with AGP 7.0, with Android Studio 4.2 and AGP 4.2 we had no problem.