How to unit test handlers whose collaborators return promises?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to unit test handlers whose collaborators return promises?

shaikhu
I'm currently learning Ratpack and need some guidance on unit testing this handler

Given a simple handler

public class UserHandler implements Handler {
    @Override
    public void handle(Context ctx) throws Exception {
      UserService userService = ctx.get(UserService.class);

      userService.findAll()
          .map(Jackson::json)
          .then(ctx::render);
    }
}

and the UserService

public interface UserService {
    Promise<List<User>> findAll();
}

what's the best way to unit test the handler?

If the UserService just returned a List<User> it would be simple enough to create a mock

UserService mockUserService  mock(UserService.class)
when(mockUserService.findAll()).thenReturn(Collections.emptyList())

and sub that in using RequestFixture.

Also I can successfully unit test the UserService directly using ExceHarness but am stuck on how to unit test the handler.

A push in the right direction would be appreciated.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to unit test handlers whose collaborators return promises?

shaikhu
Silly me.

Managed to get it working by setting the mock to return a promise e.g. when(...).thenReturn(  Promise.sync(() -> ...));

:)
Loading...