简体   繁体   中英

Same Java unit test returning different results in a plain Java project and an Android project

I have a plain old Java project and in it, a simple Java class that has a method to validate JSON strings.

public class Validator {
    public boolean isJSONValid(String jsonString) {
        try {
            new JSONObject(jsonString);
        } catch (JSONException ex) {
            try {
                new JSONArray(jsonString);
            } catch (JSONException ex1) {
                return false;
            }
        }
        return true;
    }
}

I added JUnit 5 from the Gradle file to write a unit test for this method.

test {
    useJUnitPlatform()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0'
    implementation 'org.json:json:20220924'
}

Here are the unit tests.

class ValidatorTest {
    @Test
    void jsonStringIsValid() {
        Validator validator = new Validator();
        String json =
                "{" +
                        "   \"name\":\"John\"," +
                        "   \"age\":15," +
                        "   \"gender\":\"M\"" +
                        "}";
        Assertions.assertTrue(validator.isJSONValid(json));
    }

    @Test
    void jsonStringIsInvalid() {
        Validator validator = new Validator();
        String json =
                        "   \"name\":\"John\"," +
                        "   \"age\":15," +
                        "   \"gender\":\"M\"";
        Assertions.assertFalse(validator.isJSONValid(json));
    }
}

The unit test passes correctly and everything is good here.

Then I ported the same Java class to an Android project (using Java) and added JUnit 5 again and added the same unit test over there as well.

dependencies {
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.9.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
}

tasks.withType(Test) {
    useJUnitPlatform()
}

But strangely, when I run the same tests in the Android project, the second test method jsonStringIsInvalid() passes even if I give it an invalid JSON string!

Here I have removed the starting and closing curly braces to intentionally make it an invalid JSON string. But as you can see, the test passed all the same.

在此处输入图像描述

The same tests run in the Java projects, works as expected. I added the assertTrue in the jsonStringIsInvalid() method to deliberately show the error for this example's sake.

在此处输入图像描述

The code, JUnit version are all the same. Only difference is I used IntelliJ to run the Java project and Android Studio to run the Android project.

I'm not sure why this is happening. I'd appreciate any help understanding this. I have uploaded the code below if that helps.

Java project | Android project

Found the issue. Apparently the JSON-Java library that comes bundled with the Android SDK by default is not available in the test module.

So I explicitly added the testImplementation of the library to my Gradle file.

dependencies {
    testImplementation 'org.json:json:20220924'
}

And the unit tests now work as expected.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM