Last Updated
Viewed 190,974 Times
        

I'm using Mockito 1.9.0. I want mock the behaviour for a single method of a class in a JUnit test, so I have

final MyClass myClassSpy = Mockito.spy(myInstance);
Mockito.when(myClassSpy.method1()).thenReturn(myResults);

The problem is, in the second line, myClassSpy.method1() is actually getting called, resulting in an exception. The only reason I'm using mocks is so that later, whenever myClassSpy.method1() is called, the real method won't be called and the myResults object will be returned.

MyClass is an interface and myInstance is an implementation of that, if that matters.

What do I need to do to correct this spying behaviour?

Similar Question 1 : Mocking a Spy method with Mockito

I am writing a unit test for a FizzConfigurator class that looks like:

public class FizzConfigurator {
    public void doFoo(String msg) {
        doWidget(msg, Config.ALWAYS);
    }

    public void doBar(String msg) {
        doWidget(msg, Config.NEVER);
    }

    public void doBuzz(String msg) {
        doWidget(msg, Config.SOMETIMES);
    }

    public void doWidget(String msg, Config cfg) {
        // Does a bunch of stuff and hits a database.
    }
}

I'd like to write a simple unit test that stubs the doWidget(String,Config) method (so that it doesn't actually fire and hit the database), but that allows me to verify that calling doBuzz(String) ends up executing doWidget. Mockito seems like the right tool for the job here.

public class FizzConfiguratorTest {
    @Test
    public void callingDoBuzzAlsoCallsDoWidget() {
        FizzConfigurator fixture = Mockito.spy(new FizzConfigurator());
        Mockito.when(fixture.doWidget(Mockito.anyString(), Config.ALWAYS)).
            thenThrow(new RuntimeException());

        try {
            fixture.doBuzz("This should throw.");

            // We should never get here. Calling doBuzz should invoke our
            // stubbed doWidget, which throws an exception.
            Assert.fail();
        } catch(RuntimeException rte) {
            return; // Test passed.
        }
    }
}

This seems like a good gameplan (to me at least). But when I actually go to code it up, I get the following compiler error on the 2nd line inside the test method (the Mockito.when(...) line:

The method when(T) in the type Mockito is not applicable for the arguments (void)

I see that Mockito can't mock a method that returns void. So I ask:

  1. Am I approaching this test setup correctly? Or is there a better, Mockito-recommended, way of testing that doBuzz calls doWidget under the hood? And
  2. What can I do about mocking/stubbing doWidget as it is the most critical method of my entire FizzConfigurator class?

I have a class like that is subclass of spring security CoreRememberMeService.

public class CustomRememberService extends CoreRememberMeService`{

    @Override Authentication autoLogin(....) {
         // ...
         user = processAutoLoginCookie(....) //this method is in super class
    }

When I am trying to run my JUnit, I need to stub this superclass metod 'processAutoLoginCookie' like

Mockito.spy(customeCoreRememberService);    
Mockito.doReturn(user).when(customeCoreRememberService).processAutoLoginCookie(...);

I simply don't want to call super class method during my test. but it is not working, everytime it is going into super class and not just resturning 'user' as per my expectations. can anyone please point out, why I am not able to stub that?

Suppose that I have a class like;

public class FooBar {

    public int getMethod(List<String> code){

        if(code.size() > 100)
            throw new Exception;

            return 0;
    }
}

and I have a test class like this;

@RunWith(PowerMockRunner.class)
@PrepareForTest(FooBar.class)
public class FooBarTest{

    FooBar fooBarInstance;

    @Before
    public void setUp() {

        //MockitoAnnotations.initMocks(this);
        fooBarInstance = new FooBar();   
    }

    @Test(expected = Exception.class)
    public void testGetCorrelationListCodesParameter() {

        List<String> codes = Mockito.spy(new ArrayList<String>());
        Mockito.doReturn(150).when(codes).size();
        fooBarInstance.getMethod(codes);
    }
}

How can I make this test method to throw an exception ? I've dealing for hours to do this. Well thanks anyway.

Similar Question 4 (2 solutions) : Mockito- calling real method

Similar Question 8 (1 solutions) : Exception when trying to spy with Mockito

Similar Question 9 (2 solutions) : Mockito doReturn().when() calls original method

cc