freepascal / mockito

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

Mocking method inherited from super class #75

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create the following classes
public class Car {

  private Name name;

  public Car() {
    name = new Name();
  }
  public String getType() {
    return name.getName();
  }

  public void setName(Name name) {
    this.name = name;
  }

}

public class Name {
  public String getName() {
    return "I am a car";
  }
}

public class Honda extends Car{

}

2. Now using Mockito

import org.mockito.Mockito;

import org.mockito.Mock;

import junit.framework.TestCase;

public class TestMock extends TestCase{

  @Mock private Honda honda;

  public void testCar() {
    honda.setName(null);
    Mockito.when(honda.getType()).thenReturn("Honda");
  }
}

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

Then line Mockito.when(honda.getType()).thenReturn("Honda"); produces a
nullpointer exception. This is because the class Honda inherit Car, but
does not override the getType() method. But the point of mocking here is
that I don't care what the implementation of the getType() is, just return
"Honda", how come it still goes into super class, and execute those bit of
code?

What version of the product are you using? On what operating system?
I see this in Mockito 1.5, 1.6 and 1.7. 
On Linux (Ubuntu)

Please provide any additional information below.
None 

Original issue reported on code.google.com by hzhu028@gmail.com on 20 Apr 2009 at 1:24

GoogleCodeExporter commented 9 years ago
Sorry, missed a line in testCar(), which should be:

    honda = new Honda();
    honda.setName(null);
    Mockito.when(honda.getType()).thenReturn("Honda");

Original comment by hzhu028@gmail.com on 20 Apr 2009 at 1:28

GoogleCodeExporter commented 9 years ago
But in this case honda is not a mock, but a concrete object -> honda = new 
Honda();

honda.getType() is called on a real instance and name is null, so that's why 
you're
getting NPE. Unless I'm missing something.

Original comment by bbankow...@gmail.com on 20 Apr 2009 at 5:44

GoogleCodeExporter commented 9 years ago
Ok, lets say:
//    honda = new Honda();
//    honda.setName(null);
    Mockito.when(honda.getType()).thenReturn("Honda");

this is still a null pointer. 

Original comment by hzhu028@gmail.com on 20 Apr 2009 at 6:04

GoogleCodeExporter commented 9 years ago
Where do you actually create your mock? I can see @Mock annotation, but
MockitoAnnotations.initMocks(this) is missing. If I add this line there is no 
NPE.
Please, let us know if it helps.

Original comment by bbankow...@gmail.com on 20 Apr 2009 at 7:51

GoogleCodeExporter commented 9 years ago
Thank you for your reply!

And, sorry, assume that I have MockitoAnnotations.initMocks(this) in the 
setUp() 
method so that it always runs. 

I didn't quite exactly remember the situation where I had the problem in my 
real 
production code, the above was a example trying to reproduce the problem 
(obviously 
not a good example though ;P).

How about changing the getType() from Car to be final? This time, I got a NPE 
when I 
run Mockito.when(honda.getType()).thenReturn("Honda");. Is that supposed to be 
that 
Mockito cannot mock a final method? Or I missed something? - If so, how can I 
mock a 
final method like in the above example?

Thanks very much for your time! 

Original comment by hzhu028@gmail.com on 20 Apr 2009 at 12:47

GoogleCodeExporter commented 9 years ago
Unfortunately Mockito cannot mock final methods/classes. You can check it in 
FAQ:
http://code.google.com/p/mockito/wiki/FAQ

Original comment by bbankow...@gmail.com on 20 Apr 2009 at 12:59

GoogleCodeExporter commented 9 years ago
Ok, I'm closing this one - it seems not a bug.

Original comment by szcze...@gmail.com on 20 Apr 2009 at 7:35