【问题标题】:How can I test a method which invoke protected (unwanted) methods of parent class?如何测试调用父类的受保护(不需要)方法的方法?
【发布时间】:2014-08-18 12:58:10
【问题描述】:

我被困在一个非常奇怪的案例中。 我有一些需要测试的特定代码。 这里是:

public class A {

    /*
     * The real method of real class is so big that I just don't want to test it.
     * That's why I use throwing an exception.
     */
    protected void method(Integer result) {
        throw new RuntimeException("Oops!");
    }

    protected <T> T generifiedMethod(String s, T type) {
        throw new RuntimeException("Oops!");
    }

    protected void mainMethod(Integer value) {
        throw new RuntimeException("Oops!");
    }
}

我也有一个子类:

public class B extends A {

    @Override
    protected void mainMethod(Integer value) {
        if (value == 100500) {
            Integer result = super.generifiedMethod("abc", 100);
            super.method(result);
        }
        super.mainMethod(value);
    }
}

我需要用测试覆盖子类。

我尝试了很多与 PowerMockito 的组合, 但他们都不能验证父类的受保护方法的调用。 另外,我限制只能使用 Mockito、PowerMockito 和 TestNG。

这是我的测试代码(变体之一):

@Test
public void should_invoke_parent_logic_methods_of_A_class() throws Exception {

    /* Given */
    A aSpy = PowerMockito.spy(new A());

    PowerMockito.doReturn(250).when(aSpy, "generifiedMethod", "abc", 100);
    PowerMockito.doNothing().when(aSpy, "method", 250);
    PowerMockito.suppress(method(A.class, "mainMethod", Integer.class));

    /* When */
    aSpy.mainMethod(100500);

    /* Then */
    /**
     * Here I need to verify invocation of all methods of class A (generifiedMethod(), method(),
     * and mainMethod()). But I don't need them to be invoked because their logic is unwanted
     * to be tested in case of tests for class B.
     */
}

对于如何测试 B 类的任何建议,我将不胜感激。谢谢。

更新

如果我将这段代码添加到 然后 部分

Mockito.verify(aSpy, times(3)).mainMethod(100500);
Mockito.verify(aSpy, times(1)).generifiedMethod("abc", 100);
Mockito.verify(aSpy, times(1)).method(250);

它给了我以下错误信息:

Wanted but not invoked:
a.generifiedMethod("abc", 100);

【问题讨论】:

  • 添加Mockito.verify(aSpy, times(3)).mainMethod(100500); 会产生编译错误,因为mainMethod()protected

标签: java unit-testing mockito powermock


【解决方案1】:

您是否考虑过使用变体来更改类的设计并使用组合而不是继承? 然后,您将能够模拟/监视 A 类的实例并将其注入 B 类的实例。 在这种情况下,您将能够配置您需要的任何行为。

我真的不确定 doCallRealMethod() 会不会对你有用,因为你可以选择模拟方法或调用真实方法,但不能同时进行。

【讨论】:

    【解决方案2】:

    如果我理解正确,您想测试 B 类的方法“mainMethod”。因此,您应该模拟 B 类的对象,而不是 A 类的对象。试试这个:

    /* Given */
    B b = Mockito.mock(B.class);
    
    //use this to skip the method execution
    Mockito.doNothing().when(b).generifiedMethod(Mockito.anyString(), Mockito.any());
    Mockito.doNothing().when(b).method(Mockito.anyInt());
    
    //or use this to return whatever you want when the methods are called
    Mockito.doReturn(new Object()).when(b).method(Mockito.anyInt());
    Mockito.doReturn(new Object()).when(b).generifiedMethod(Mockito.anyString(), Mockito.any());
    

    然后你可以从 B 调用 mainMethod,已经知道其他方法会返回什么。

    【讨论】:

    • 可能不会,因为方法受到保护,测试无法访问它们
    猜你喜欢
    • 2013-02-04
    • 2022-08-05
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2011-10-09
    • 1970-01-01
    相关资源
    最近更新 更多