Export to GitHub

mockito - issue #25

Can't stub spied object


Posted on Oct 2, 2008 by Swift Panda

I can not stub method for spying object. There is thrown NullPointerException if I use anyString() as an argument of the method and inside the method the argument is used.

Example: I have following class:

11: public class Main { 12: 13: public String sayHello(String name) { 14: int len = name.length(); 15: return "Hello "+name+" ["+len+"]"; 16: } 17: 18: }

and test for such class:

13: @Test 14: public void testSayHello() { 15: Main main = new Main(); 16: Main spy = spy(main); 17: stub(spy.sayHello(anyString())).toReturn("Hello!"); 18: System.out.println(spy.sayHello("Chris"));
19: verify(spy).sayHello("Chris");
20: }

Now when I run the test I get: java.lang.NullPointerException at Main.sayHello(Main.java:11) at Main$$FastClassByCGLIB$$2479d9.invoke(<generated>) at org.mockito.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) at org.mockito.internal.MockHandler.intercept(MockHandler.java:92) at org.mockito.internal.creation.MethodInterceptorFilter.intercept(MethodInterceptorFilter.java:45) at Main$$EnhancerByCGLIB$$7d3682ac.sayHello(<generated>) at MainTest.testSayHello(MainTest.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66) at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

I use: mockito: 1.5 java: 1.6 OS: Windows XP

regards yaSiu

Comment #1

Posted on Oct 2, 2008 by Massive Monkey

looking at it...

Comment #2

Posted on Oct 2, 2008 by Massive Monkey

Very interesting case. anyString() method returns null but also anyString() makes a mock remember that the argument can be any string. I will think about making argument matchers (e.g. anyString()) return not nulls in order to accommodate spies.

In meantime please use doReturn() API for such case. Sometimes you have to use doReturn() for stubbing spies due to the way Mockito syntax is implemented. You can find more info here: http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html#spy(T)

Comment #3

Posted on Oct 12, 2008 by Massive Monkey

fixed in trunk

Comment #4

Posted on Apr 19, 2009 by Massive Monkey

(No comment was entered for this change.)

Comment #5

Posted on Oct 7, 2010 by Massive Monkey

This issue was closed by revision 831de24497.

Status: Fixed

Labels:
Type-Defect Priority-Medium Milestone-Pre1.7