Reputation: 141
I'm trying to write a JUnit test case for a RESTeasy web service. I'd like to use the RESTeasy MockDispatcherFactory for this, in combination of not relying on any data access layers.
In my previous test case authoring, I've used Mockito to mock the data access, but I'm having trouble doing this with RESTeasy's MockDispatcherFactory...
Service Class:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("")
public class Service {
private StubDao stubDao;
public Service (){
this.stubDao = new StubDao();
}
public Service (StubDao stubDao){
this.stubDao = stubDao;
}
@GET
@Produces(MediaType.TEXT_HTML)
public String get(){
return stubDao.getTheValue();
}
}
Data Access:
public class StubDao {
private String value;
public StubDao(){
}
public String getTheValue(){
//Stubbed Data Access
return value;
}
public void setTheValue(String v){
this.value = v;
}
}
Unit Test:
import java.net.URISyntaxException;
import junit.framework.Assert;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory;
import org.junit.Test;
public class TestService {
@Test
public void testService() throws URISyntaxException{
POJOResourceFactory factory = new POJOResourceFactory(Service.class);
//I Need to use Mockito to mock the StubDao object!!!
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addResourceFactory(factory);
MockHttpRequest request = MockHttpRequest.get("");
MockHttpResponse response = new MockHttpResponse();
//here my exception is thrown
dispatcher.invoke(request, response);
System.out.println(response.getContentAsString());
// but I expect the response to be 404 (which works outside the mock setup
Assert.assertEquals(response.getStatus(), 404);
}
}
Usually I'd use Mockito to mock the data access like so:
Set up the mock
@Before
public void setup() {
StubDao stubDao = new StubDao();
}
Define the mock
when(stubDao.getTheValue()).thenReturn("the mocked value");
However, RESTeasy's mocking creates a new instance of the service class internally. My question is, how do I insert the mocked data access into the constructor of the service???
Any help is appreciated!
Upvotes: 2
Views: 11145
Reputation: 1140
Alternatively you can use testfun-JEE for running a lightweight JAX-RS (based on RESTeasy and TJWS) inside your test and using testfun-JEE's JaxRsServer junit rule for building REST requests and asserting the responses.
testfun-JEE supports injection of other EJBs as well as mockito mock objects into your JAX-RS resource class.
Upvotes: 2
Reputation: 141
Found the answer thanks to another post (Resteasy Server-side Mock Framework)
Using the following allowed me to create an instance of the service class and set the data access:
dispatcher.getRegistry().addSingletonResource(svc);
Instead of:
dispatcher.getRegistry().addResourceFactory(factory);
Upvotes: 9