简体   繁体   English

相同的 Java 单元测试在普通 Java 项目和 Android 项目中返回不同的结果

[英]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.我有一个普通的旧 Java 项目,其中有一个简单的 Java class,它具有验证 JSON 字符串的方法。

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.我从 Gradle 文件中添加了 JUnit 5 来为此方法编写单元测试。

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.然后我将相同的 Java class 移植到 Android 项目(使用 Java)并再次添加 JUnit 5 并在那里也添加了相同的单元测试。

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!但奇怪的是,当我在 Android 项目中运行相同的测试时,即使我给它一个无效的 JSON 字符串,第二个测试方法jsonStringIsInvalid()通过了!

Here I have removed the starting and closing curly braces to intentionally make it an invalid JSON string.在这里,我删除了开始和结束大括号,故意使其成为无效的 JSON 字符串。 But as you can see, the test passed all the same.但如您所见,测试仍然通过了。

在此处输入图像描述

The same tests run in the Java projects, works as expected.相同的测试在 Java 项目中运行,按预期工作。 I added the assertTrue in the jsonStringIsInvalid() method to deliberately show the error for this example's sake.为了这个例子,我在jsonStringIsInvalid()方法中添加了assertTrue来故意显示错误。

在此处输入图像描述

The code, JUnit version are all the same.代码,JUnit版本都是一样的。 Only difference is I used IntelliJ to run the Java project and Android Studio to run the Android project.唯一不同的是我使用 IntelliJ 运行 Java 项目,使用 Android Studio 运行 Android 项目。

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 | Java 项目| Android project Android项目

Found the issue.发现了问题。 Apparently the JSON-Java library that comes bundled with the Android SDK by default is not available in the test module.显然,默认情况下与 Android SDK 捆绑在一起的 JSON-Java 库在测试模块中不可用。

So I explicitly added the testImplementation of the library to my Gradle file.所以我明确地将库的testImplementation添加到我的 Gradle 文件中。

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

And the unit tests now work as expected.并且单元测试现在按预期工作。

Source资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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