【问题标题】:PowerMockito doesn't match overloaded method when return types differ当返回类型不同时,PowerMockito 与重载方法不匹配
【发布时间】:2015-08-26 10:48:52
【问题描述】:

这是series...的第 3 部分...

我(仍在)尝试使用 PowerMockito 检查 bar(Alpha, Baz) 是否调用了 bar(Xray, Baz)bar(Xray, Baz)private) - 没有实际调用后者,因为我的 MCVE 类 Foo 在下面。请注意,bar(Alpha, Baz) 不返回任何内容,而其他两个返回 String,并且我知道我 should 可能测试 Foo 的工作原理,而不是 如何嗯>...

public class Foo {
    private String bar(Xray xray, Baz baz) { return "Xray"; }

    private String bar(Zulu zulu, Baz baz) { return "Zulu"; }

    public void bar(Alpha alpha, Baz baz) { // this one returns nothing
        if(alpha.get() instanceof Xray) {
            System.out.println(bar((Xray) alpha.get(), baz));
            return;
        } else if(alpha.get() instanceof Zulu) {
            System.out.println(bar((Zulu)alpha.get(), baz));
            return;
        } else {
            return;
        }
    }
}

当所有方法具有相同的返回类型时,用户 kswaughs solved the issue 用于私有重载方法。并且elsewhere 建议将when() 方法与Method 对象一起使用...但是,既然我已经定义bar(Alpha, Baz) 以使用与其他方法不同的返回类型,所有这些都再次崩溃了:

@RunWith(PowerMockRunner.class)
public class FooTest {

    @Test
    public void testBar_callsBarWithXray() throws Exception {
        Baz baz = new Baz(); //POJOs
        Alpha alpha = new Alpha();
        alpha.set(new Xray());

        Foo foo = new Foo();
        Foo stub = PowerMockito.spy(foo);

        Method m = Whitebox.getMethod(Foo.class, "bar", Xray.class, Baz.class);

        PowerMockito.doReturn("ok").when(stub, m);

        stub.bar(alpha, baz); // fails here - even though that then calls stub.bar(Xray, Baz);

        // Testing if bar(Xray, Baz) was called by bar(Alpha, Baz)
        PowerMockito.verifyPrivate(stub, times(5)).invoke("bar", any(Xray.class), any(Baz.class));
    }
}

所有美丽的例外:

org.mockito.exceptions.base.MockitoException: 
'bar' is a *void method* and it *cannot* be stubbed with a *return value*!
Voids are usually stubbed with Throwables:
    doThrow(exception).when(mock).someVoidMethod();
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. The method you are trying to stub is *overloaded*. Make sure you are calling the right overloaded version.
2. Somewhere in your test you are stubbing *final methods*. Sorry, Mockito does not verify/stub final methods.
3. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

    at FooTest.testBar_callsBarWithXray(FooTest.java:31)

使用.withArguments(any(Xray.class), any(Baz.class)) 似乎没有什么不同。

虽然恰到好处,但不幸的是,异常并没有说明如何通过我的设置来实现第 1 点。有什么想法吗?

【问题讨论】:

  • 我认为您缺少 @PrepareForTest(Foo.class) 类类型注释

标签: java powermock overloading private-members powermockito


【解决方案1】:

这里,问题不在于返回类型不同,而在于重载方法。使用 MemberMatcher.method() 而不是 WhiteBox.getMethod, 出于测试目的,在公共栏方法顶部添加了 sysout 语句。

public void bar(Alpha alpha, Baz baz) { // this one returns nothing

System.out.println("public bar");

if(alpha.get() instanceof Xray) {
    System.out.println(bar((Xray) alpha.get(), baz));
    return;
} else if(alpha.get() instanceof Zulu) {
    System.out.println(bar((Zulu)alpha.get(), baz));
    return;
} else {
    return;
}
}

下面是测试方法,我们使用MemberMatcher时Foo.class需要PrepareForTest。

@Test
 public void testBar_callsBarWithXray() throws Exception {
     Baz baz = new Baz(); //POJOs
     Alpha alpha = new Alpha();
     alpha.set(new Xray());

     Foo stub = PowerMockito.spy(new Foo());

     Method m = MemberMatcher.method(Foo.class,
                "bar",
                Xray.class, Baz.class);

     PowerMockito.doReturn("ok")
        .when(stub, m )
        .withArguments(Matchers.any(Xray.class), Matchers.any(Baz.class));

     stub.bar(alpha, baz);

     PowerMockito.verifyPrivate(stub, Mockito.times(1)).invoke("bar", Matchers.any(Xray.class), Matchers.any(Baz.class));
     // Mockito's equivalent for a public method: verify(stub, times(1)).bar(any(Xray.class), any(Baz.class));
 }

output is :
public bar
ok

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多