java Powermock和Mockito。避免在模拟和存根同一类时对类进行静态初始化
假设我有一个名为Util的类,带有静态字段:
public class Util {
public static field = Param.getValue("param1");
}
类参数如下所示:
public class Param {
public static field = SomeClass.getValue("someValue");
}
我想嘲弄和嘲弄Param。getValue(“param1”)位于Util内部,但同时我希望抑制Param类的静态初始化。我怎样才能做到这一点
这是我第一次尝试,但没用
@RunWith(PowerMockRunner.class)
@PrepareForTest({Param.class})
@SuppressStaticInitializationFor("py.com.company.Param")
public class Test {
@Test
public void testSomeMethod() {
PowerMockito.mockStatic(Param.class);
when(Param.getValue("value1")).thenReturn("someValue1");
}
}
# 1 楼答案
这对我有用。我没有得到任何输出,如果没有
SomeClass#getValue
与: