Mocking / stubbing of native methods

2,388 views
Skip to first unread message

Raphael André Bauer

unread,
Jan 12, 2011, 5:34:43 AM1/12/11
to moc...@googlegroups.com
Hi,


I have a use case where it would be really cool to mock/stub/spy a
native method. Below two code snipplets.
However, I always get a java.lang.UnsatisfiedLinkError:
ExampleDto.getName()Ljava/lang/String;

I did not find evidence that Mockito is able to mock/stub/spy native
methods - is that true? (Btw. I am using Mockito 1.8.5 currently).


Thanks :)

Best,

Raphael


EXAMPLE CODE:
//The class to mock:
public class ExampleDto extends JavaScriptObject {

public ExampleDto() {};

public native final String getName() /*-{
return this.name;
}-*/;
}

//Mocking code using a spy:
ExampleDto exampletDto
= Mockito.spy(new ExampleDto());

Mockito.doReturn("").when(exampleDto).getName();
// this results in a java.lang.UnsatisfiedLinkError:
ExampleDto.getName()Ljava/lang/String;

Felix Leipold

unread,
Jan 12, 2011, 8:58:44 AM1/12/11
to moc...@googlegroups.com
You could introduce an interface that is implemented by your native
method and then mock the interface.
On a more general note I find it quite confusing, that you have a
native method on something called a DTO.

Cheers,
Felix

> --
> 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.
>
>

Raphael André Bauer

unread,
Jan 12, 2011, 9:23:19 AM1/12/11
to moc...@googlegroups.com
On Wed, Jan 12, 2011 at 2:58 PM, Felix Leipold <felix....@gmail.com> wrote:
> You could introduce an interface that is implemented by your native
> method and then mock the interface.
> On a more general note I find it quite confusing, that you have a
> native method on something called a DTO.

I completely agree. We got some constraints in our app that lead to
such strange "DTO"s. Using interfaces is also a good idea, however I
would like to omit that extra bit of code.

So. Basically there is no way to mock native methods using current
Mockito... Right?


Thanks,

Raphael

Johan Haleby

unread,
Jan 12, 2011, 9:58:50 AM1/12/11
to moc...@googlegroups.com
If you cannot refactor you can use PowerMock's Mockito API to mock the native method.

/Johan

Raphael André Bauer

unread,
Jan 12, 2011, 10:12:17 AM1/12/11
to moc...@googlegroups.com
On Wed, Jan 12, 2011 at 3:58 PM, Johan Haleby <johan....@gmail.com> wrote:
> If you cannot refactor you can use PowerMock's Mockito API to mock the
> native method.

Thanks!


Cheers,

Raphael

Reply all
Reply to author
Forward
0 new messages