Below is my JUnit that describes the problem in code.
Thanks,
Karl
-----
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import org.junit.Test;
/**
* How do you mock an abstract class and still call the real code for
the non-abstract methods?
*/
public class MockitoTest {
public static abstract class Failure {
private Displayer d;
public void addDisplayer(Displayer d) {
this.d = d;
}
public void log(String s) {
d.display(s);
}
}
public interface Displayer {
void display(String s);
}
/**
* mock() fails to call real code.
*/
@Test
public void testMock() {
Failure f = mock(Failure.class);
Displayer d = mock(Displayer.class);
f.addDisplayer(d);
f.log("Some text.");
verify(d).display("Some text.");
}
/**
* cannot spy() a mock().
*/
@Test
public void testSpy() {
Failure f = spy(mock(Failure.class));
Displayer d = mock(Displayer.class);
f.addDisplayer(d);
f.log("Some text.");
verify(d).display("Some text.");
}
}
Ok. Thanks, just wanted to be sure that I wasn't missing something.
> What's wrong with subclass ?
Nothing, I guess, but I didn't want to pollute the repository with a
dummy class containing no-ops. I want to test the functionality of
the abstract versus the children, as it is not possible to make all
methods final and it's important to ensure the correct functioning of
each method.
Karl
Ok. Thanks, just wanted to be sure that I wasn't missing something.
> You cannot.
> Mockito doesn't support partial mocking and the best option is to
> create the subclass.
Nothing, I guess, but I didn't want to pollute the repository with a
> What's wrong with subclass ?
dummy class containing no-ops.
I want to test the functionality of
the abstract versus the children,
as it is not possible to make all
methods final and it's important to ensure the correct functioning of
each method.
Karl