Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

静态代码块 #301

Open
ChenJunGui opened this issue Nov 10, 2022 · 1 comment
Open

静态代码块 #301

ChenJunGui opened this issue Nov 10, 2022 · 1 comment

Comments

@ChenJunGui
Copy link

比如:连接一个redis,采用是静态代码块的方式。测试的时候是否能屏蔽了?不然每次测试该类都要加载连接

@linfan
Copy link
Collaborator

linfan commented Nov 10, 2022

支持直接Mock静态代码块内里的调用,具体一个实际的例子:

业务代码里使用静态代码块加载本地系统库,在运行单元测试的时候希望将它忽略。

public class JNIClass {

    static {
        System.loadLibrary("native-lib");
    }

    // 以下具引入的方法省略
    public native String xxxx(... ...);

}

在测试时候时候Mock掉这个loadLibrary()调用,改为打印一行文本(这里是用独立的Mock类,也可以写在Test类里的Mock内部类里面):

public class JNIClassMock {

    @MockInvoke(targetClass = System.class, targetMethod = "loadLibrary")
    private void load(String libname) {
        System.err.println("loadLibrary " + libname);
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants