验证在子类上调用此方法时是否调用了重写的超类方法 [英] Verify that overriden superclass method is called when invoking this method on subclass

查看:153
本文介绍了验证在子类上调用此方法时是否调用了重写的超类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用此示例显示我的问题:

我有一个方法 foo 的类。该类有一个子类来覆盖这个方法。

子类'方法调用超类'方法。我可以验证吗?

我不想测试超类中的 foo 。我只需要验证它是否被调用。
我知道重构可以帮助(赞成组合而不是继承等)但是我无法做到这一点。

有没有办法实现我的需要?

以下是我尝试过的简单示例

I'll show my problem using this example:
I have a class with a method foo. That class has a subclass which overrides this method.
Subclass' method calls superclass' method. Can I verify that?
I don't want to test what foo in superclass does. I just need to verify that it was called. I know that refactoring could help (favour composition over inheritance, etc) but I am unable to do that.
Is there any way to achieve what I need?
Below is simple example of what I've tried

package tmp;
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.Mockito.times;

public class Example {
    @Test
    public void test() {
        // given
        ChildClass childClass = new ChildClass();
        ChildClass spyChildClass = Mockito.spy(childClass);
        // when
        spyChildClass.foo(100);
        // then
        Mockito.verify((BaseClass) spyChildClass, times(1)).foo(101);
    }
}

abstract class BaseClass {
    public void foo(int n) {
        System.out.printf("BaseClass.foo(%d)%n", n);
    }
}

class ChildClass extends BaseClass {
    @Override
    public void foo(int n) {
        System.out.printf("ChildClass.foo(%d)%n", n);
        super.foo(n + 1);
    }
}

这就是结果:

ChildClass.foo(100)
BaseClass.foo(101)

Argument(s) are different! Wanted:
childClass.foo(101);
-> at tmp.Example.test(Example.java:19)
Actual invocation has different arguments:
childClass.foo(100);
-> at tmp.Example.test(Example.java:16)

Expected :childClass.foo(101);
Actual   :childClass.foo(100);
   <Click to see difference>

显然这不是我想看到的。

Obviously it's not what I wanted to see.

我无法修改 BaseClass 。我不想测试 BaseClass (我不负责)。我甚至不知道它究竟是做什么的。我只需要验证它的方法是否被调用。还有别的不是我的问题。这是维持 BaseClass 的人的问题。

I can't modify BaseClass. I don't want to test BaseClass (I am not responsible for it). I don't even neet to know what exactly it does. I just need to verify that its method was called. Wnything else is not my problem. Its the problem of people who maintain BaseClass.

推荐答案

测试<该类的强>行为,而不是实现

编写测试以便调用该方法并且预期做一点事。接下来检查对象现在代表您现在期望它代表的内容。

Write the test such that the method is called and is expected to do something. Next check the object now represents what you now expect it to represent.

如果 BaseClass.foo 预计会增加一些计数器除以100,但是 Subclass.foo 将一些计数器递增50然后调用超类,验证计数器现在是150而不是50。

If BaseClass.foo is expected to increment some counter by 100, yet Subclass.foo increments some counter by 50 then calls the superclass, verify that the counter is now 150 and not just 50.

不要偷看它们 - 它们可能会随着时间的推移而改变。测试行为。除了增加计数器之外,方法 foo 可以做其他事情 - 检查对象的状态而不是它的作用。

Don't peek the how - they may change over time. Do test the behaviour. The method foo may do other things besides increase counters - check the state of the object not what it did.

这篇关于验证在子类上调用此方法时是否调用了重写的超类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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