easyforgood / mockito

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

Stubbing SuperClass Method Called From Overridden SubClass Method #472

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. Create the following classes
public class SuperClass{
  private String name;

  public boolean method(String name) {
       System.out.println("SuperClass Called");
       if("INVALID".equals(name))
       {
           return false;           
       }
       this.name = name;
       return true;
  }
}

public class SubClass extends SuperClass{

  private String subName;

  public void method(String name) {
    boolean success = super.method(name);
    System.out.println("SubClass Called");
    if(!success){
        subName = name;
    }
}

2. Now using Mockito

public class TestMock{

   private static final String DUMMY_STR = "DummyString";

  @Before
  public void testMethod() {   

     SubClass sub = mock(SubClass.class,CALLS_REAL_METHODS);

     doCallRealMethod().doReturn(false).when(sub).generate(DUMMY_STR);

     sub.method(DUMMY_STR);
  }
}

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

I expect only SubClass getting called and a console output "SubClass Called".

But what I see is even SuperClass gets called and prints console message. 

What version of the product are you using? On what operating system?
I see this in Mockito 1.9.5 
On Windows XP/Eclipse

Please provide any additional information below.
None 

Original issue reported on code.google.com by hellovid...@gmail.com on 14 Feb 2014 at 9:42

GoogleCodeExporter commented 8 years ago
Corrected a line:

doCallRealMethod().doReturn(false).when(sub).method(DUMMY_STR);

Original comment by hellovid...@gmail.com on 14 Feb 2014 at 9:47

GoogleCodeExporter commented 8 years ago
Corrected Annotation:
Replace @Before with @Test

Original comment by hellovid...@gmail.com on 14 Feb 2014 at 9:50

GoogleCodeExporter commented 8 years ago
Return types of methods in SubClass and SuperClass are different. 
Are you sure that there is no more changes?

Original comment by albers...@gmail.com on 24 Feb 2014 at 11:22

GoogleCodeExporter commented 8 years ago
@alberskib is correct, the signature of method is different, so there is no 
overriding there, which means you have to stub both methods.

And anyway as you are using the answer that call real method, the bytecode 
still reference the method of the super class, there's is no polymorphism when 
the `super` keyword is used.

Also I strongly advise you to avoid partial mocks, it can be a 
maintainance/evolutivity hell. You may want to refactor your design to use 
common behavioral patterns  of the GoF, like the *strategy pattern*

I'm invalidating the issue, please raise a comment if you think it's really an 
issue.

Thanks for reporting though.
Brice

Original comment by brice.du...@gmail.com on 4 Mar 2014 at 9:36

GoogleCodeExporter commented 8 years ago
Apologies for the delayed response.  Signature of both the methods is same.  It 
was a typo when I posted above example.

Original comment by hellovid...@gmail.com on 5 Mar 2014 at 3:08