如何测试调用父类的受保护(不需要的)方法的方法? [英] How can I test a method which invoke protected (unwanted) methods of parent class?

查看:458
本文介绍了如何测试调用父类的受保护(不需要的)方法的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm stuck in a very weird case. I have some specific code which I need to test. Here it is:

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);
    }
}

我需要用测试覆盖子类.

I need to cover the child class with tests.

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

I was trying a lot of combinations with PowerMockito, but none of them can verify invocation of protected methods of parent class. Also, I have a restriction on use only Mockito, PowerMockito and TestNG.

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

Here is my test code (one of variants):

@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级的任何建议,我将不胜感激.

I would be appreciate for any suggestions how to test class B. Thanks.

更新

Update

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

If I add into Then section this code

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);

推荐答案

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

Did you consider variant to change design of your classes and use composition instead of inheritance ? Then you will be able to just mock / spy instance of class A and inject it to instance of class B. In such case you will be able to configure whatever behavior that you need.

我真的不确定doCallRealMethod()会为您带来麻烦,因为您可以选择模拟方法或调用真正的方法,但不能同时选择两者.

I really not sure doCallRealMethod() will make trick for you, cause you have option to mock method or invoke real one, but not both simultaneously.

这篇关于如何测试调用父类的受保护(不需要的)方法的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆