- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 PowerMock 而不是 Mockito 来模拟静态 void 方法,但效果不太好。
我的示例代码:
BlackTempleTest.java
package com;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import com.BlackTempleTest.Illidan;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Illidan.class, EvilBrother.class })
public class BlackTempleTest {
Answer<Object> defaultAnswer = new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
System.out.println("Haha!");
return null;
}
};
@Test(expected = AssertionError.class)
public void testIllidanFight() throws Exception {
Illidan.startFight();
}
@Test
public void testCheatOnIllidanFight() throws Exception {
PowerMockito.mockStatic(Illidan.class, defaultAnswer);
Illidan.startFight();
}
@Test(expected = AssertionError.class)
public void testEvilBrotherFight() throws Exception {
EvilBrother.startFight();
}
// dont work
@Test
public void testCheatOnEvilBrotherFight() throws Exception {
PowerMockito.mockStatic(EvilBrother.class, defaultAnswer);
EvilBrother.startFight();
}
static class Illidan {
static void startFight() {
Assert.fail("You are not prepared!");
}
}
}
邪恶兄弟.java
package com;
import com.BlackTempleTest.Illidan;
public class EvilBrother extends Illidan {
}
我的问题是,嵌套类按预期使用 @PrepareForTest 和 PowerMockito.mockStatic 的组合进行模拟,但如果该类位于其自己的类文件中,这些语句将不起作用。
如何修复此测试?
编辑:
与
PowerMockito.doAnswer(defaultAnswer).when(EvilBrother.class, "startFight");
可以通过 Powermock 管道传输错误调用,但会执行 Assert.fail。
java.lang.AssertionError: You are not prepared!
at org.junit.Assert.fail(Assert.java:88)
at com.BlackTempleTest$Illidan.startFight(BlackTempleTest.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1873)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:773)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:753)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
at org.powermock.api.mockito.internal.expectation.PowerMockitoStubberImpl.when(PowerMockitoStubberImpl.java:106)
at com.BlackTempleTest.testCheatOnEvilBrotherFight(BlackTempleTest.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
最佳答案
解决方案是,即使你调用 EvilBrother.startFight,你也必须模拟 Illidan 而不是 EvilBrother,因为该方法是继承的。
@Test
public void testCheatOnEvilBrotherFight() throws Exception {
PowerMockito.mockStatic(Illidan.class, defaultAnswer);
EvilBrother.startFight();
}
关于java - PowerMock mockStatic 没有捕获模拟的静态 void 方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29275179/
我有一个具有静态变量、静态 block 和静态函数的公共(public)类。我正在测试我的测试器类中的一个静态函数(比如 x),我通过在测试器类的类级别(Powermock)使用 @SuppressS
我正在尝试编写一个 TestNg 测试用例。这是为了测试当客户端抛出异常时会发生什么。我正在使用 PowerMock 来模拟客户端调用。下面是我的测试方法(只有UnitTest Snipped,而不是
我正在尝试模拟我的测试方法的内部方法调用 我的类(class)看起来像这样 public class App { public Student getStudent() { MyDAO dao
我正在尝试编写一个需要 的测试机器人电动 2.2 和 PowerMock ,因为被测代码依赖于一些 Android 库和第三方库以及我需要模拟的最终类。 鉴于我被迫通过以下方式使用 Robolectr
无论如何,tom 是否使用 Powermock 模拟类中的 super 方法调用? 例如: public void processRetrievedData() { super.processRe
运行Junit测试期间出现以下错误。 java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor at
我想使用 PowerMock 模拟以下代码: ConnectionFactory rabbitMqFactory = createFactory(); com.rabbitmq.client.Conn
我收到 PowerMock 错误,但我没有使用 PowerMocking。我正在使用正常的模拟。这是我要测试的类(class): import java.util.List; import org.a
使用 TestNg + PowerMock + Mockito 时遇到 java.lang.ExceptionInInitializerError: Caused by: java.lang.Null
有没有办法注释@PowerMockIgnore 以忽略整个包,除了这个包中的一个类? 我希望做一些事情,例如: @PowerMockIgnore({ "foo.bar.ignore*\DoNotIgn
当我尝试执行以下简化的 JUnit 测试时,它成功了,但我收到一条错误消息:首先创建所有测试实例时不支持通知! @RunWith(PowerMockRunner.class) @PowerMockRu
我正在测试一些遗留代码并尝试使用 PowerMock 来模拟静态方法调用。我很快发现它与类加载器混淆了,这不是我认为有资格深入研究的问题。仅供引用,我的问题类似于 this但是在我的情况下发布的解决方
public class TestStatic { public static int methodstatic(){ return 3; } } @Test @Pre
我想通过Maven使用最新版本的powermock库(1.6.5)。但是我的包无法编译,因为 Maven 发现依赖收敛错误。下面您可以看到有 2 个不同版本的 org.objenesis:objene
我在 Stack 上看到过这个问题的另一个例子,但没有答案。谁能根据经验(或任何其他深奥的手段)告诉我这是否可能做到?我已经遵循了我能找到的所有模拟静态方法的示例,但还没有找到适用于抽象类中的静态方法
我在模拟 Calendar.getInstance() 时遇到问题。正如您现在一样,此方法返回一个日历 - 我正在 mock 的对象。 现在我的代码如下所示: @RunWith(PowerMockRu
我有一个带有静态方法的简单类,该方法通常会抛出空指针: public class MyClass { private static String s; public static fi
我有多个 HttpPost 请求,如下所示: try (CloseableHttpClient httpclient = HttpClients.createDefault()) { Http
我想通过Maven使用最新版本的powermock库(1.6.5)。但是我的包无法编译,因为 Maven 发现依赖收敛错误。下面您可以看到有 2 个不同版本的 org.objenesis:objene
我正在使用 PowerMock、EasyMock 组合。 如果没有另外指定,如何让每个模拟对象默认返回“null”? 现在,我必须对模拟对象的每个方法进行“预期”(或在mockito世界中的“何时”)
我是一名优秀的程序员,十分优秀!