freepascal / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Can't stub spied object #25

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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(MethodIntercepto
rFilter.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.j
ava:99)
    at
org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java
:81)
    at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunne
r.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(TestClassMeth
odsRunner.java:66)
    at
org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.jav
a:35)
    at
org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java
:42)
    at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunne
r.java:34)
    at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)
    at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReferen
ce.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.jav
a:196)

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

regards
yaSiu

Original issue reported on code.google.com by krzysiek.yaSiu@gmail.com on 2 Oct 2008 at 1:40

GoogleCodeExporter commented 9 years ago
looking at it...

Original comment by szcze...@gmail.com on 2 Oct 2008 at 3:44

GoogleCodeExporter commented 9 years ago
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)

Original comment by szcze...@gmail.com on 2 Oct 2008 at 6:17

GoogleCodeExporter commented 9 years ago
fixed in trunk

Original comment by szcze...@gmail.com on 12 Oct 2008 at 8:24

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 19 Apr 2009 at 7:40

GoogleCodeExporter commented 9 years ago
This issue was closed by revision 831de24497.

Original comment by szcze...@gmail.com on 7 Oct 2010 at 5:19