在代码库中,我具有以下结构:
abstract class Bar{
    public Bar(){
        ....
    }
    ....        
    public Bar(int x, int y){
    }
    ....
}
Bar然后由 扩展Foo。
abstract class Foo extends Bar{
    public Foo(){
      super();
        ....
    }
    public Foo(int x){
      super(x,0);  // call parent's specific constructor
      ....
    }
    ....
}
我尝试了以下无法编译的 jUnit 测试用例:
class FooTest{
    Foo _foo;
    @Test
    void testFooConstructor(){
        new Expectations(){
            Bar bar;
            {
                bar = new Bar(anyInt,0); // error, obviously Bar cannot be instantiated.
            }
        }
        _foo = new Foo(anyInt){ // empty implementation
            //Override any abstract methods
        }
    }
}
我写了上面的方法,因为我看到了这个 SO question,但是抽象类可能没有被启动,因此它失败了。
此外,我还尝试过:
class FooTest{
    Foo _foo;
    @Test
    void testFooConstructor(){
        _foo = new Foo(anyInt){ // empty implementation
            //Override any abstract methods
        }
        new Expectations(){
            Bar bar;
            {
                invoke(bar,"Bar",anyInt,0); //Invocations.invoke
            }
        }
        invoke(_foo,"Foo",anyInt);
    }
}
但是,我的测试结果是:
java.lang.IllegalArgumentException:找不到兼容的方法:bar(int,int) at unit.src.com.test.FooTest$1.(行号)
我怎样才能达到预期的效果?有没有办法实现这个测试?