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

增加Mock方法的上下文控制参数注入机制 #17

Closed
linfan opened this issue Dec 24, 2020 · 1 comment
Closed

增加Mock方法的上下文控制参数注入机制 #17

linfan opened this issue Dec 24, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@linfan
Copy link
Collaborator

linfan commented Dec 24, 2020

当同一测试用例的执行过程中包含对同一个被Mock目标方法的多次调用时,当前TestableTool工具类提供的能力依然无法便捷的区分这几次调用并进行差异化处理。考虑增加一种注入额外控制参数到Mock上下文的机制,完善TestableMock对Mock流程控制能力。

方案设计:

// 在测试用例设置控制参数
@Test
public void test() {
    TestableTool.set(“mark”, 1);  // 注入控制参数
    demoInstance.method(123);   // 测试场景1
    ... ...
    TestableTool.set(“mark”, 2);  // 修改控制参数值
    demoInstance.method1(234);   // 测试场景2
    ... ...
}

// 在Mock方法中读取控制参数
@MockMethod
private int demoMethod() {
    if (TestableTool.get(“mark”) == 1) {
        return 321;
    } else {
        return 432;
    }
}
@linfan
Copy link
Collaborator Author

linfan commented Dec 30, 2020

已在v0.4.6版本实现。

当前实现机制较简单,效果类似于在Mock类当中添加一个用于存储上下文信息的Map类型的成员对象,但请尽量使用此机制而非自定义对象传递附加的Mock参数,以便在未来支持Mock方法复用的新版本中,获得更好的版本升级兼容性。

@linfan linfan closed this as completed Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant