freepascal / mockito

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

mock of class method calls real method in Restlet framework #68

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
execute this code in a junit test class

import org.restlet.Client;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
...
public class TestClass {

public void testMethod {
Client client = mock(Client.class);
StringBuilder queryValue = new StringBuilder();
        queryValue.append("uri=").append("http://www.boomroosvis.nl");
Reference reference = new Reference(sruServer);
reference.addQueryParameter("query", queryValue.toString());

Request request = new Request(Method.GET, reference);
Response response = new Response(request);

response.setEntity(sb.toString(), MediaType.APPLICATION_XML);
response.setStatus(Status.SUCCESS_OK);

when(client.get(any(Reference.class))).thenReturn(response);    

2. The call on the mocked client call internally on the real Client class
the following code:

public final Response get(Reference resourceRef) {
        return handle(new Request(Method.GET, resourceRef));
    }

3. This gives the following error:
org.mockito.exceptions.base.MockitoException: 
Cannot stub a void method with a return value!
Voids are usually stubbed with Throwables:
    doThrow(exception).when(mock).someVoidMethod();

... stacktrace

What is the expected output? What do you see instead?
Expected is that the mock just uses the return value when called. 

What version of the product are you using? On what operating system?
1.7 core

Original issue reported on code.google.com by HvdSch...@gmail.com on 1 Apr 2009 at 12:09

GoogleCodeExporter commented 9 years ago
First, check out if client.get() is not final.

I'll try to reproduce it to figure out why this exception is thrown.

Original comment by szcze...@gmail.com on 1 Apr 2009 at 1:34

GoogleCodeExporter commented 9 years ago
I'm not sure why there's a 'cannot stub a void method' exception. Are you sure 
this
is the exact code?

Original comment by szcze...@gmail.com on 1 Apr 2009 at 2:21

GoogleCodeExporter commented 9 years ago
the get method is indeed final. But the call within is to a an abstract method 
handle
(..). Is there way to mock that call ??

Original comment by HvdSch...@gmail.com on 1 Apr 2009 at 3:11

GoogleCodeExporter commented 9 years ago
Yes, the code is correct. The client code calls within to it's parent who has an
abstract method --> public abstract void handle(Request request, Response 
response)

Original comment by HvdSch...@gmail.com on 1 Apr 2009 at 3:17

GoogleCodeExporter commented 9 years ago
I guess there is no way to test final methods. This issue can be closed for my 
part

Original comment by HvdSch...@gmail.com on 2 Apr 2009 at 7:44

GoogleCodeExporter commented 9 years ago
That's true, Mockito doesn't do final methods.

Original comment by szcze...@gmail.com on 2 Apr 2009 at 8:27