Java PowerMock-mockStatic没有';t捕获模拟的静态void方法调用
我尝试在Mockito上使用PowerMock模拟静态void方法,但效果不太好 我的示例代码: BlackTempleTest.javaJava PowerMock-mockStatic没有';t捕获模拟的静态void方法调用,java,unit-testing,junit,mockito,powermock,Java,Unit Testing,Junit,Mockito,Powermock,我尝试在Mockito上使用PowerMock模拟静态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; i
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!");
}
}
}
我的问题是,使用@PrepareForest和PowerMockito.mockStatic的组合按预期模拟嵌套类,但是如果该类在其自己的类文件中,这些语句就不起作用
a如何修复此测试
编辑:
与
可以通过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)
解决方案是,你必须模仿伊利丹而不是邪恶兄弟,即使你调用邪恶兄弟.startFight,因为方法是继承的
@Test
public void testCheatOnEvilBrotherFight() throws Exception {
PowerMockito.mockStatic(Illidan.class, defaultAnswer);
EvilBrother.startFight();
}
我有相同的错误。但是我的模拟类没有从另一个类继承。请发布您的代码。没有你,我无法帮助你。我解决了我的问题。谢谢你的回复。在某些情况下,单个测试框架并不包括您需要的所有必要工具。这就是为什么在上面的示例中使用PowerMock的原因。
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)
@Test
public void testCheatOnEvilBrotherFight() throws Exception {
PowerMockito.mockStatic(Illidan.class, defaultAnswer);
EvilBrother.startFight();
}