Thursday, January 9, 2014

Impractical use of doCallRealMethod() when object being spy

Oh no! Another mistake in this morning. Although the code sound true but the code doesn't sound logic. Take a look at the code snippet from previous note as an example:
public class FileClass {
 public void funcB() {
  try {
   throw new ClassNotFoundException();
  catch( ClassNotFoundException e ) {
Due to the misunderstanding between mock and spy, the following test code isn’t really practical because ClassFile is already in spy mode, the use of doCallRealMethod() is just a waste, thus invoking the real instance is more than enough. doCallRealMethod() is usable if and only if FileClass is a mock.
 public void testFuncB() throws ClassNotFoundException {
  FileClass fc = Mockito.spy(new FileClass());

No comments: