如何使用PowerMockito模拟嵌套的静态方法?

时间:2018-10-04 07:47:40

标签: java methods static mockito powermockito

如何使用PowerMockito模拟嵌套的静态方法? 以下是一个有关如何使用它的非常简单的示例。

班级

public class SomeClass {
    public static String someMethodA(){
        //some very important codes here
        return someMethodB();
    }
    private static String someMethodB(){
        return someMethodC();
    }
    private static String someMethodC(){
        return "Some Text";
    }
}

已经尝试了以下操作,但没有成功:

PowerMockito.mockStatic(SomeClass.class);
PowerMockito.stub(PowerMockito.method(SomeClass.class, "someMethodB")).toReturn("Some Other Text");

我想按原样运行someMethodA(),这就是为什么我想存根someMethodB()的原因。反正我能做到这一点吗?希望有一种方法无需修改访问修饰符,因为我正在使用的代码是旧代码。

1 个答案:

答案 0 :(得分:0)

您可以PartitionSort(L) if head[L] != tail[L] then //Choose the pivot node, first node or last node is the option pivot := tail[L] //Partition step, we distribute nodes of the linked list into three sublists curr := head while curr != NULL do if key[curr] < key[pivot] then pushBack(curr,Less) else if key[curr] = key[pivot] then pushBack(curr,Equal) else pushBack(curr,Greater) end if end if curr := next[curr] end while // Here me make sure that we partitioned linked list correctly // We should set next of tail pointers and prev of head pointers to NULL //Now we do recursive calls on sublists with keys not equal to the pivot key PartitionSort(Less) PartitionSort(Greater) // Now we concatenate sublists if tail[Less] != NULL then next[tail[Less]] := head[Equal] else head[Less] := head[Equal] end if if head[Equal] then prev[head[Equal]] = tail[Less] tail[Less] = tail[Equal] end if if tail[Less] != NULL then next[tail[Less]] := head[Greater] else head[Less] := head[Greater] end if if head[Greater] then prev[head[Greater]] = tail[Less] tail[Less] = tail[Greater] end if L := Less end if 私有静态方法。

spy中进行了测试

JUnit4