joepadde / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Mockito doesn't seem to blank out superclass methods when you mock a sublclass #463

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.
class Foo {
  private String field = null;

  public String foo(String input) {
    return field.toLowerCase() + input.toLowerCase();
  }
}

class Bar extends Foo {
}

2. in your tests...
Bar mockBar = mock(Bar.class);
when(mockBar.foo("WHATEVER")).thenReturn("ABCWHATEVER");

What is the expected output? What do you see instead?

expectation is the method foo("whatever") gets intercepted by mockito
and the real implementation never gets called, and my test passes.

instead the superclass method is not overridden and I get a null pointer error 
because  field.toLowerCase() is called with field = null. this error is thrown 
at the when().thenReturn() line above.

What version of the product are you using? On what operating system?
1.9.5, linux

Please provide any additional information below.

It's interesting to note that one workaround is to define in 
Bar

@Override
public String foo(String input) {
  return super.foo(input);
}

Perhaps in mockito's code you are reflecting only on the class's directly 
defined methods, instead of all methods including those inherited from 
superclasses.

Original issue reported on code.google.com by fri...@google.com on 14 Nov 2013 at 3:53

GoogleCodeExporter commented 8 years ago
This is a known issue when the parent of the mocked class is not public, 
Mockito cannot override correctly the superclass methods. Unfortunately there's 
little we can do about that unless it's fixable in CGLIB/ASM.

Sorry

Cheers,
Brice

Original comment by brice.du...@gmail.com on 4 Dec 2013 at 2:30