[mockito] NullPointerException thrown when mocking Groovy class

已查看 1,480 次
跳至第一个未读帖子

Teo Hui Ming

未读,
2010年4月21日 05:17:162010/4/21
收件人 mockito
1) Create C:\test\Foo.groovy that uses mockito
[code]
import static org.mockito.Mockito.*

public class Foo {
def bar() { "bar" }

public static void main(String[] args) {
def mock_foo = mock(Foo.class)
mock_foo.bar()
verify(mock_foo).bar()
}
}
[/code]


2) Compile Foo.groovy to .class
[code]
C:\test>groovyc -v
Groovy compiler version 1.6.0
Copyright 2003-2008 The Codehaus. http://groovy.codehaus.org/

C:\test> groovyc Foo.groovy
[/code]


3) Run Foo.class, NullPointerException error is thrown
[code]
C:\test> java -version
java version "1.6.0_13"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)

C:\test> java -cp "%GROOVY_HOME%/embeddable/groovy-all-1.6.0.jar;
%GROOVY_HOME%/lib/mockito-all-1.8.4.jar;." Foo
Exception in thread "main" java.lang.NullPointerException
at
org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:
39)
at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:
43)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:
116)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:
120)
at Foo.main(Foo.groovy:8)
[/code]

Does it mean Mockito currently cannot mock Groovy classes, or I am
missing something?

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.

szczepiq

未读,
2010年4月21日 16:51:372010/4/21
收件人 moc...@googlegroups.com
Not sure what's the problem. I don't use Mockito with groovy but in general it should just work :)

Szczepan

Teo Hui Ming

未读,
2010年4月22日 00:53:492010/4/22
收件人 mockito
On Thu, Apr 22, 2010 at 4:51 AM, szczepiq <szcz...@gmail.com> wrote:
> Not sure what's the problem. I don't use Mockito with groovy but in general
> it should just work :)

But it doesn't :)

Does anyone use Mockito for Groovy-based project?

--
Teo Hui Ming

Chris Bartling

未读,
2010年4月22日 08:05:492010/4/22
收件人 moc...@googlegroups.com
I haven't used mockito on Groovy-based projects, but I have done Groovy based projects in the past with copious use of test doubles.  I'm also a big fan of mockito when using Java.  In Groovy, you have meta-object protocol or MOP that allows you to use meta-programming techniques.  Using the MOP is generally preferred to using a traditional Java mocking framework.  I'm fairly certain that mockito could be used to mock a dependency (which your code example did not include), but most Groovy-ists typically will use the MOP instead.  You should also look at MockFor() and StubFor() which is included in the standard Groovy Development Kit (GDK) and GMock.



HTH,

-- chris bartling --

Teo Hui Ming

未读,
2010年4月22日 08:24:222010/4/22
收件人 mockito、chris.bartling
Thanks Chris, I will try GMock.

When using MockFor(), the main issue is, I couldn't figure out how to:

1. mock 2 collaborator objects
2. inject them into one object under-test
3. call a method in object under-test
3. verify both mocked objects was called correctly

For example:

def mocker1 = new MockFor(Collaborator1.class)
def mocker2 = new MockFor(Collaborator2.class)
mocker1.use { .. } // what about mocker2.use {} ?

Perhaps this is off-topic in mockito list. :)

Grégory Fouquet

未读,
2010年4月22日 12:17:062010/4/22
收件人 moc...@googlegroups.com
Hi List,

2010/4/22 Teo Hui Ming <teohu...@gmail.com>

On Thu, Apr 22, 2010 at 4:51 AM, szczepiq <szcz...@gmail.com> wrote:
> Not sure what's the problem. I don't use Mockito with groovy but in general
> it should just work :)

But it doesn't :)

Does anyone use Mockito for Groovy-based project?

I remember I ran into the same problem.

When groovy invokes a method on a groovy objects, it uses the object's metaclass, retreived with the groovyObject.getMetaClass() method.

Unfortunately, on a Mockito mock object, the method is proxied and returns null, hence the error.

Grégory Fouquet


回复全部
回复作者
转发
0 个新帖子