Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>UPDATE: I wrote the stuff below, and then moments later discovered .thenCallRealMethod() which allows you to effectively perform partially stubbing. Mockito authors recommend you use refactoring to separate dependencies into different classes; but they do provide the means to partially stub. I have added a test method to demonstrate this approach, and leave my original comments.</p> <p>ORIGINAL: I really like Mockito, but this is the one place where EasyMock wins out. I have two solutions for you that don't involve Mockito. The first is to override methodB on your testing instance. The other is to partially mock with EasyMock:</p> <pre><code>import org.junit.Test; import static org.junit.Assert.*; import static org.easymock.EasyMock.*; public class PartialMockTest { class ClassX { String methodA(String arg) {return methodB(arg);} String methodB(String arg) {return "toto";} } @Test public void MockitoOnClassX(){ ClassX classx = mock(ClassX.class); when(classx.methodB("hiyas")).thenReturn("tomtom"); when(classx.methodA(anyString())).thenCallRealMethod(); String response = classx.methodA("hiyas"); assertEquals("tomtom",response); } @Test public void OverrideOnClassX() { ClassX classx = new ClassX(){@Override String methodB(String arg){return "tomtom";}}; String response = classx.methodA("hiyas"); assertEquals("tomtom",response); } @Test public void PartialMockOnClassX() throws NoSuchMethodException { ClassX classx = createMockBuilder(ClassX.class).addMockedMethod("methodB").createMock(); expect(classx.methodA("hiyas")).andReturn("tomtom"); replay(classx); String response = classx.methodA("hiyas"); assertEquals("tomtom",response); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload