Capacytron
Capacytron

Reputation: 3739

Mockito mocked object suddenly throws NPE

I have a mock:

 static MyGroovyBasedHttpClient createClient(){
    MyGroovyBasedHttpClient client = mock(MyGroovyBasedHttpClient.class);
    Answer<SimpleResponse> methodAnswer = createGenericAnswer();

    when(client.myMethod(anyString(), anyInt(),anyString())).thenAnswer(methodAnswer);
    return client;
}

And I have method invocation

def response = client.myMethod(stringParam1, intParam2, stringParam3)

And I get NPE on client.myMethod invocation If i do

println client // mock for MyGroovyBasedHttpClient with hashcode

so 100% client is initialized and not NULL I even hace smoke assertions that mocked components are not null. Also I have smoke on static MyGroovyBasedHttpClient createClient() Smoke checks that mocked client does return my custom Answer

How can I debug such problem?

UPD: Look like this is my problem: https://code.google.com/p/mockito/issues/detail?id=303

Upvotes: 0

Views: 304

Answers (1)

Kasper Ziemianek
Kasper Ziemianek

Reputation: 1349

There is a problem with mockito and groovy, you need additional dependency or you can mock only interfaces.

For more info visit : https://github.com/cyrusinnovation/mockito-groovy-support

Issue 303 about mockito problem with groovy classes : https://code.google.com/p/mockito/issues/detail?id=303

Upvotes: 1

Related Questions