我有一个名为的Java类MyClass,我想用JUnit进行测试.methodA我要测试的公共方法调用私有方法methodB,在同一个类中确定要遵循的条件路径.我的目标是为不同的路径编写JUnit测试methodA.此外,methodB调用服务,所以我不希望它在运行JUnit测试时实际执行.
模拟methodB和控制其返回的最佳方法是什么,以便我可以测试'methodA'的不同路径?
我更喜欢在编写模拟时使用JMockit,所以我对任何适用于JMockit的答案都特别感兴趣.
这是我的示例类:
public class MyClass {
public String methodA(CustomObject object1, CustomObject object2) {
if(methodB(object1, object2)) {
// Do something.
return "Result";
}
// Do something different.
return "Different Result";
}
private boolean methodB(CustomObject custObject1, CustomObject custObject2) {
/* For the sake of this example, assume the CustomObject.getSomething()
* method makes a service call and therefore is placed in this separate
* method so that later an …Run Code Online (Sandbox Code Playgroud)