In the code base I have the following structure:
abstract class Bar{
public Bar(){
....
}
....
public Bar(int x, int y){
}
....
}
Bar
is then extended by Foo
.
abstract class Foo extends Bar{
public Foo(){
super();
....
}
public Foo(int x){
super(x,0); // call parent's specific constructor
....
}
....
}
I have tried the following jUnit test case, which cannot compile:
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
}
}
}
I have written the above approach because I saw this SO question, but the abstract class may not be initiated, and hence it fails.
Additionally, I have also tried:
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);
}
}
However, my test result is:
java.lang.IllegalArgumentException: No compatible method found: Bar(int,int) at unit.src.com.test.FooTest$1.(line number)
How can I achieve the desired result? Is there a way to implement this test?