首页 > 解决方案 > 如何使用 Junit 和 mockito 为这个类编写单元测试?

问题描述

下面是我想使用 Junit 和 Mockito 编写单元测试的课程。我无法做到这一点。

该接口加载一个本地库,然后使用类方法调用本地库方法。我需要通过模拟本机库方法来为这些方法编写单元测试。我发现这很棘手,因为使用 InterfaceA.INSTANCE.method1() 调用了本机方法。我如何嘲笑它?

package x.x.x;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class ClassB {
    
    public interface InterfaceA extends Library {
        
        InterfaceA INSTANCE = (InterfaceA) Native.load("ClassA.api", InterfaceA.class);
        int interfaceMethod1();
        int interfaceMethod2(String string1);
    }    

    public boolean method1(){
        int result = InterfaceA.INSTANCE.interfaceMethod1();
        return result != 0;
    }

    public boolean method2(String string1){
        int result = InterfaceA.INSTANCE.interfaceMethod2(string1);
        return result != 0;
    }
}

标签: javajunitmockitopowermockito

解决方案


推荐阅读