1

This single line of snippet of converting string to JSONArray is returning as null, any ideas?

JSONArray("""[{"id":1,"first":"John","last":"Doe"}]""")

enter image description here

Here are my dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'

    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

    testImplementation 'junit:junit:4.13'
    testImplementation 'org.mockito:mockito-inline:2.13.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    implementation 'androidx.test.espresso:espresso-idling-resource:3.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
    androidTestImplementation 'androidx.test:rules:1.3.0-alpha05'
    implementation 'com.google.android.material:material:1.1.0'
}

Subsequent issues. Calling localhost from SUT using JUnit.

enter image description here

1 Answer 1

1

I'm assuming you must be running unitTests which are present under module/tests in Android project.

If you try to use or access the Android related code in JUnit tests then it is not expected to work until and unless you've not added the following line of code in your build.gradle file of the module.

testOptions {
    unitTests.returnDefaultValues = true
    unitTests.includeAndroidResources = true
}

And once you add the above configuration, any access to Android methods is expected to return their default value, no matter what you're passing to them.

Anything related to android tests needs to be written under androidTest

For more info.

http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-

Sign up to request clarification or add additional context in comments.

5 Comments

you are right, the class is a SUT and is running under a test, however, I'm still getting null, still checking if I got everything right, any ideas?
please check my dependencies too
after some research I have added in testImplementation 'org.json:json:20140107', however, there's another problem, I'm testing on localhost via http://10.0.2.2:8080 (see second screenshot), I'd appreciate your input there as well. Thanks
It seems your project is not able to fetch the dependency. Download it manually for now and then try to test it once if that is helping you resolve your issue. Do download manually and use that as a dependency refer to stackoverflow.com/questions/29402155/…
I revisited the project 2 days after, added the jar as per the suggested link, it worked, I commented the local jar path to force it to fail and it still worked, deleted the jar file from libs and its continuing working, not sure what happened.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.