验证是否调用了受保护的超级方法

ari*_*rin 7 java verification junit jmockit

我有以下结构:

class Bar{
  ....
  protected void restore(){
    ....
  }

  ....
}
Run Code Online (Sandbox Code Playgroud)

该课程扩展Foo如下:

class Foo extends Bar{
    ....

    @Override
    public void restore(){  //valid override
        super.restore();

        ....
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的jUnit测试中,我想测试一下,当foo.restore()调用时,super.restore()随后会调用它.因此,下面是我的jUnit测试方法:

class FooTest{
  @Tested
  Foo _foo;

  @Test
  void testRestore(final Bar bar){

    new Expectations(){{
      bar.restore(); times = 1; // Error! bar.restore() not visible
    }};

    Deencapsulation.invoke(_foo,"restore");
  }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我的测试无法编译.原因是1)restore()父母protected和2)FooTest并且Foo在一个单独的项目(因此文件夹)中一起存在Bar.

反正有没有达到预期的测试?我已经检查了jMockit教程(过去几个月多次)并且没有看到类似的测试(在Google上进行搜索也是如此).


更新

在响应的帮助下,我理解强制执行子类调用super并不是最佳实践,但这不是我的实现,我仍然需要测试它.我仍在寻找一种方法来强制执行我的jUnit测试,以检查是否正在调用父项.

Rog*_*rio 2

以下测试应该有效:

public class FooTest
{
    @Tested Foo _foo;

    @Test
    void restoreInFooCallsSuper(@Mocked final Bar bar)
    {
        new Expectations() {{
            invoke(bar, "restore");
        }};

        _foo.restore();
    }
}
Run Code Online (Sandbox Code Playgroud)