Question

I am new to Mockito and trying to handle a scenario.

I have a class that i am spying on;

//Production Code Starts

class LoginRequest {
    final Callable<String> buildRequest() {
        callABC();
        callXYZ();
        Callable<String> callableObject = createCallable();
        return  callableObject;
    }
    final Callable<String> buildRequest(CallBack callBackObject) {
        callABC();
        callXYZ();
        Callable<String> callableObject = createCallable(callBackObject);
        return  callableObject;
    }
}

LoginRequest createLoginRequest() {
    LoginRequest loginRequest = new LoginRequest();
    return loginRequest; 
}

Callable<String> callable = loginRequest.buildRequest();
Netowrk.submit(callable);

//Production Code ends

Now, What I want is, that when call to

loginRequest.buildRequest() 

is made it should instead be propagated to the parameter version of the method; so instead of calling

buildRequest() 

it should call

buildRequest(callBackObject);

So I have tried the following;

//Test Code Starts
LoginRequest createLoginRequest() {
    final LoginRequest loginRequest = new LoginRequest();

    final LoginRequest spy = Mockito.spy(loginRequest);

    final CallBack callBackObject = new CallBack()//my CallBack object

    Mockito.when(spy.buildRequest()).thenAnswer(new Answer<Callable<?>>() {

        @Override
        public Callable<String> answer(InvocationOnMock invocation)
                throws Throwable {

                Callable<String> callable = spy.buildRequest(callBackObject);
            return callable;
        }
    });
    return spy;
}

I was expecting that when

loginReuest.buildRequest() 

be called from production code it would propagated to Answer.answer() {} and then to

buildRequest(callBackObject);

from there.

However what is happening is, that when call to original

loginReuest.buildRequest() 

method from the production code is made, it actually calls the original

buildRequst()

and from there call to

callABC()

is made, which in turn calls my anonymous

Answer.answer();

and end up calling

callable = spy.buildRequest(callBackObject);

and enters in recursive calls and then stack overflow.

Is there a better way better to do this kind of thing.

I appreciate your help

Thanks Big O

Was it helpful?

Solution

You can use PowerMock to test final methods:

  • Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case.
  • Use the @PrepareForTest(ClassWithFinal.class) annotation at the class-level of the test case.
  • Use PowerMock.createMock(ClassWithFinal.class) to create a mock object for all methods of this class (let's call it mockObject).
  • Use PowerMock.replay(mockObject) to change the mock object to replay mode.
  • Use PowerMock.verify(mockObject) to change the mock object to verify mode.

You have some examples in this page:

https://code.google.com/p/powermock/wiki/MockFinal

You can also use mockito with powermock:

https://code.google.com/p/powermock/wiki/MockitoUsage13

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top