@Mock(answer = Answers.RETURNS_DEEP_STUBS) not working with PowerMockRunner

3,890 views
Skip to first unread message

Behrang Saeedzadeh

unread,
Aug 4, 2014, 12:49:49 AM8/4/14
to powe...@googlegroups.com
Hi all,

I want to use PowerMockito for mocking some static methods, etc. However when I use PowerMockRunner instead of MockitoJUnitRunner, The answer types for @Mock annotations are ignored:

import com.day.cq.search.Query;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.jcr.Node;
import java.util.Iterator;


@RunWith(PowerMockRunner.class)
public class SampleTest extends AbstractServiceTest {

    @Mock(name = "Mock Query", answer = Answers.RETURNS_DEEP_STUBS)
    private Query query;

    @Mock(name = "Mock Nodes")
    private Iterator<Node> queryResult;

    @Test
    public void deepStubsTest() {
        Mockito.when(query.getResult().getNodes()).thenReturn(queryResult);

        System.out.println(query.getResult().getNodes());
    }
}


This throws NullPointerException

java.lang.NullPointerException
at foo.SampleTest.deepStubsTest(SampleTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)

However if I change the runner to MockitoJUnitRunner everything works as expected.

Is this a known bug/issue?

Thanks.

Johan Haleby

unread,
Aug 4, 2014, 2:29:21 AM8/4/14
to powe...@googlegroups.com
Yeah that's a known issue. The reason why it's not trivial to implement is that you would expect that for example final instances returned by the mock would work when using PowerMock. However PowerMock requires you to prepare unmockable classes for test using the @PrepareForTest annotation and would limit the use of RETURNS_DEEP_STUBS.

/Johan


--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

Johan Haleby

unread,
Aug 4, 2014, 3:32:03 AM8/4/14
to powe...@googlegroups.com
I've implemented simple support for this now in master. This means that it won't work with deep stubs or mocks that are final but it works with regular classes the same way Mockito does. I suppose it's better than nothing. Also added support for "name" and extra interfaces. Please try it out by depending on version 1.5.6-SNAPSHOT after having added the following Maven repo:

<repositories>
        <repository>
            <id>sonatype</id>
            <snapshots />
        </repository>
</repositories>

Reply all
Reply to author
Forward
0 new messages