- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
即使我关注了 manual我似乎无法使用 PowerMock 模拟静态方法。我正在尝试模拟一个单例神类。
测试代码如下:
@RunWith(PowerMockRunner.class)
@PrepareForTest(GodClass.class)
public class SomeTestCases {
@Test
public void someTest() {
PowerMockito.mockStatic(GodClass.class);
GodClass mockGod = mock(GodClass.class);
when(GodClass.getInstance()).thenReturn(mockGod);
// Some more things mostly like:
when(mockGod.getSomethingElse()).thenReturn(mockSE);
// Also tried: but doesn't work either
// when(GodClass.getInstance().getSomethingElse()).thenReturn(mockSE);
Testee testee = new Testee(); // Class under test
}
}
和受测者:
class Testee {
public Testee() {
GodClass instance = GodClass.getInstance();
Compoment comp = instance.getSomethingElse();
}
}
但是,这不起作用。 Debug模式显示 instance
为 null
。有什么不同之处?
(是的,我知道代码很糟糕,但它是遗留的,我们希望在重构之前进行一些单元测试)
最佳答案
我只是基本上输入了您在这里得到的内容,它对我来说工作正常。
public class GodClass
{
private static final GodClass INSTANCE = new GodClass();
private GodClass() {}
public static GodClass getInstance()
{
return INSTANCE;
}
public String sayHi()
{
return "Hi!";
}
}
public class Testee
{
private GodClass gc;
public Testee() {
gc = GodClass.getInstance();
}
public String saySomething()
{
return gc.sayHi();
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(GodClass.class)
public class GodClassTester
{
@Test
public void testThis()
{
PowerMockito.mockStatic(GodClass.class);
GodClass mockGod = PowerMockito.mock(GodClass.class);
PowerMockito.when(mockGod.sayHi()).thenReturn("Hi!");
PowerMockito.when(GodClass.getInstance()).thenReturn(mockGod);
Testee testee = new Testee();
assertEquals("Hi!", testee.saySomething());
}
}
关于java - 使用 powermockito 模拟静态类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21048437/
我只想模拟类中的一个静态方法,所有其他方法都应该像真实对象一样工作。 代码: public class ArrTest { public static int myMethod (int ar
我在使用 PowerMockito 模拟 whenNew(File.class) 时遇到问题。这是我要测试的方法: public void foo() { File tmpFile = new
我正在尝试模拟静态方法 Thread.sleep(1);在调用时返回 InterruptedException。我发现了一个似乎解决了我的问题的 SO 问题,但是在将我的代码设置为与该问题的答案相同后
我似乎无法克服这个问题。我正在尝试模拟一个带有 1 个参数的重载方法 class ClassWithOverloadedMethod { private boolean isValid(Cla
考虑以下(简化的)枚举: MyEnum { ONE public int myMethod() { // Some complex stuff return 1
当我尝试使用字符串输入模拟静态方法时,当我给出特定字符串时,模拟 stub 就会被执行,但是当我使用anyString()时,它不会按预期工作。 public class Foo { publ
我在一些单元测试中使用 PowerMockito,但遇到了问题。我正在尝试测试一种创建一系列线程并运行它们的方法。在每个线程内,创建一个我需要期望的对象,并返回我自己的模拟对象(它发出 http 请求
我有以下类,在构造函数中,我调用另一个构造函数来构建该类的字段: public class ClassIWantToTest { private ClassIWantToMock anothe
您好,我有这个 PowerMockito 测试,它会抛出 UnfinishedStubbingException @RunWith(PowerMockRunner.class) @PrepareFor
我有一些静态方法可以使用 Mockito + PowerMock 进行模拟。一切都是正确的,直到我尝试模拟一个只抛出异常的静态方法(并且什么都不做)。 我的测试类是这样的: 顶部: @RunWith(
测试片段: void somefunction() { try { aMock.doSomething(); bMock.another(); } finally {
我正在尝试使用 PowerMockito 捕获输入中传递给模拟对象的参数,这是代码: //I create a mock object ClassMocked mock = PowerMockito.
要测试的类 public class Randomer { public int get() { return (int) Math.random() + 1; } }
我正在编写 JUnit 测试以验证静态方法 (MyClass.myMethod()) 从未在方法流中被调用。我试着做这样的事情: PowerMockito.verifyStatic(Mockito
我想检查是否使用一组特定的参数调用了特定的构造函数,并检查参数是否正确。 该类使用 java (...) 的任意参数,如下所示: public class MyClass{ public My
我正在测试一个运行直到AtomicBoolean值发生变化的任务类。我使用 PowerMockito 是因为 AtomicBoolean 实例的 get() 方法是 final方法。 MyTaskTe
我有以下 jar : javax.servlet-api-3.1.9.jar junit-4.10.jar mockito-all-1.10.19.jar mockito-core-1.10.19.j
在单元测试中,如何忽略对方法的调用,如下所示? void methodToBeTested(){ // do some stuff methodToBeSkipped(parame
我正在尝试测试一些严重依赖静态方法调用的遗留代码。 基本上,我有一个类 A,它有方法 b() 和 c()。 A.b() 返回 void,A.c() 返回一个值。 如果真正的 A.b() 被调用,被测类
我正在使用 PowerMockito 测试静态方法,但不幸的是得到了ClassCastException。注意完全确定我是否没有遵循语法规则。 待测试代码: List proxyPrefs = (Li
我是一名优秀的程序员,十分优秀!