Mocking HTTPClient with Mockito, still runs the mocked method, not sure why

2,383 views
Skip to first unread message

Jim Barrows

unread,
Jul 5, 2010, 3:02:07 PM7/5/10
to specs-users
This may or may not be the right forum, since Mockito could be the
culprit here.
I'm attempting to mock org.apache.http.impl.client.DefaultHttpClient
and so I have:

val httpClient = mock [DefaultHttpClient]
httpClient.execute(postMethod) returns expectedResponse

The execute method gets executed. The exception that I see is:
[info] null (AbstractHttpClient.java:517)
[info]
org.apache.http.impl.client.AbstractHttpClient.getProtocolProcessor(A
bstractHttpClient.java:517)
[info]
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:678)
[info]
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:625)
[info]
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:603)
[info] test.MetaRestResourceSpecs$$anonfun$1$$anonfun$apply$1$
$anonfun$apply
$2.apply(MetaRestResourceSpecs.scala:44)

So, my first question is.. Am I doing something wrong?
Second is this Mockito, or Specs? or DefaultHttpClient?
Third, if it's specs, how do I fix it?

etorreborre

unread,
Jul 5, 2010, 4:54:28 PM7/5/10
to specs-users
Hi Jim,

This is a "classical" Mockito gotcha. Mockito doesn't mock final
methods but executes them. I would advise you to use only the
HttpClient interface in your code (because that can be mocked ok) and:

- pass the DefaultHttpClient implementation to your production code
- use the mock[HttpClient] for the specification

Eric.

Jim Barrows

unread,
Jul 6, 2010, 9:59:00 AM7/6/10
to specs...@googlegroups.com
Ok.  I think I actually use some of DefaultHttpClient's methods in my code.  I'll see if I can factor that out though.

Thanks a lot!

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




--
James A Barrows

Reply all
Reply to author
Forward
0 new messages