【问题标题】:PowerMock mockStatic didn't catch mocked static void method callPowerMock mockStatic 没有捕捉到模拟的静态 void 方法调用
【发布时间】:2015-05-30 06:37:01
【问题描述】:

我尝试在 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;
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!");
        }
    }
}

EvilBrother.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)

【问题讨论】:

    标签: java unit-testing junit mockito powermock


    【解决方案1】:

    解决方案是,你必须模拟伊利丹而不是 EvilBrother,即使你调用 EvilBrother.startFight 因为方法是继承的。

    @Test
    public void testCheatOnEvilBrotherFight() throws Exception {
        PowerMockito.mockStatic(Illidan.class, defaultAnswer);
    
        EvilBrother.startFight();
    }
    

    【讨论】:

    • 我有同样的错误。但是我的模拟类没有从另一个类继承。
    • 请发布您的代码。没有它就无法为您提供帮助。
    • 我解决了我的问题。感谢您的回复。 stackoverflow.com/questions/50361736/…
    • 在某些情况下,单个测试框架并不包含您需要的所有必要工具。这就是为什么在上面的示例中使用 PowerMock 的原因。
    猜你喜欢
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2011-11-20
    • 1970-01-01
    • 2014-02-03
    相关资源
    最近更新 更多