【问题标题】:JUnit Test coverage when there exists a JNI API call存在 JNI API 调用时的 JUnit 测试覆盖率
【发布时间】:2020-08-31 05:05:14
【问题描述】:

我正在编写 JUnit 测试用例。我有一个包含调用 JNI API 的 API 的类。

public class Test {

    public native int getId_native();

    public void doSomething() {
        // do init
        int id = getId_native();
        // Get name by Id
    }

}

我正在为doSomething() API 编写单元测试。我面临的问题是,只要调用JNI API(getId_native()),就会抛出 UnsatisfiedLinkError 并在我的 JUnit 测试中捕获并测试结束。在本机函数之后,控制不会进一步测试代码行。这减少了项目中所有类似方法的代码覆盖率。

由于本地方法在同一个类中声明,我也无法模拟该类。

有人可以帮我解决这个问题吗?

谢谢。

【问题讨论】:

  • 您可以将本机调用移到一个单独的类中,该类自己提供查找/本机功能以方便测试,或者尝试使用 powermock 并简单地让本机调用什么都不做(并抑制任何静态如果需要,可以阻止)

标签: java junit code-coverage junit4 junit5


【解决方案1】:

您需要在测试期间加载本机库。您可以通过各种方式加载 dll。例如:

System.load("path to your dll with native method");

如果原始库不适合测试,则构建具有单元测试预期功能的模拟原生 dll。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2020-11-03
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多