Question

I am newbie to gwt, mvp and easymock. I am building an application using MVP pattern inwhich presenter is calling the remote RestService. I want to test presenter function which is calling remote service using EasyMock. Currently, my code looks like

    expect(applicationView.get()).andReturn(5);       
    applicationPresenter.setView(applicationView);

    replay(applicationView);

    MyRestService mService = createNiceMock(MyRestService.class);       
    applicationPresenter.setMyRestService(mService);
    applicationPresenter.fetchAllBooks();
    verify(applicationView);

I run test and test passes successfully. Am i doing right? How to test the response because service is a RestService interface which returns response in a MethodCallback. My service interface looks like:

@Path("/rest-service")
public interface MyRestService extends RestService {
@Path(value="/getAllBooks")
@GET
void getAllBooks(MethodCallback<List<Book>> callback);

Please help that my procedure for presenter and server calls testing is right? How to test response and pass the values to Application view? Do I need to pass values to view or just mock the values?

Regards,

Was it helpful?

Solution

I'm not massively knowledgeable on REST services, but a web service that starts with get and returns void seems very odd to me.

That aside, you will need to expect the call to getAllBooks as your service is a mock. From the look of it, the fetchAllBooks method instantiates the MethodCallBack and then provides it to mock service. So, if you want to make changes to it so that it is in some expected state when the fetchAllBooks method continues, you could use andAnswer() from the expectation.

Something like this should do the trick:

    MyRestService mService = createNiceMock(MyRestService.class);  
            mService.getAllBooks(EasyMock.isA(MethodCallcack.class));
    EasyMock.expectLastCall().andAnswer(new IAnswer() {

        @Override
        public Object answer() throws Throwable {
            final Object[] currentArguments = EasyMock.getCurrentArguments();
            MethodCallback callback = ((MethodCallback)currentArguments[0]);
            //Set whatever you need to on your MethodCallback so that the data makes it back.

            return null;
        }

    });

    //Don't forget to replay
    EasyMock.replay(mService);

There is a good stack overflow post about this sort of thing here too

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top