JUnit测试覆盖率当存在JNI API调用时

huangapple go评论82阅读模式
英文:

JUnit Test coverage when there exists a JNI API call

问题

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

public class Test {

    public native int getId_native();

    public void doSomething() {
        // 进行初始化
        int id = getId_native();
        // 通过Id获取名称
    }

}

我正在为doSomething() API编写单元测试。我遇到的问题是,一旦调用了JNI API(getId_native()),就会抛出UnsatisfiedLinkError,并在我的JUnit测试中捕获该错误,从而结束测试。控制不会继续前进以测试原生函数后的代码行。这导致项目中所有类似方法的代码覆盖率降低。

由于原生方法在同一个类中声明,我也无法对该类进行模拟。

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

谢谢。

英文:

I am writing JUnit test cases. I have a class that contains APIs which call JNI APIs.

public class Test {

    public native int getId_native();

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

}

And I am writing unit test for doSomething() API. The problem that I am facing is, as soon as the JNI API(getId_native()) is called, UnsatisfiedLinkError is thrown and caught in my JUnit test and the test ends. Control does not proceed further to test the lines of code after the native function. This reduced the code coverage in all similar methods in the project.

Since the native methods are declared in the same class, I am unable to mock the class too.

Can someone help me how to overcome this issue?

Thanks.

答案1

得分: 1

你需要在测试过程中加载本地库。您可以通过多种方式加载DLL。例如:

System.load("您的包含本地方法的DLL文件路径");

如果原始库不适合于测试,那么请构建模拟的本地DLL,其中包含您在单元测试中所期望的功能。

英文:

You need to have native library loaded during your tests. You can load dll with various ways. For example:

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

If original lib is not suitable for testing then build mock native dll with functionality expected in your unit tests.

huangapple
  • 本文由 发表于 2020年8月31日 13:05:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63665060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定