Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda Support to tidy up expectations #210

Open
sazzer opened this issue Mar 2, 2018 · 1 comment
Open

Lambda Support to tidy up expectations #210

sazzer opened this issue Mar 2, 2018 · 1 comment

Comments

@sazzer
Copy link

sazzer commented Mar 2, 2018

Currently, when you write expectations you've got to deal with the fact that the signatures could throw. This means that your test methods need to indicate all of these exceptions even if they will never happen.

Java 8 Lambdas could ease this. For example:

EasyMock.expect(() -> {
    EasyMock.expect(target.callThatThrows())
        .andThrow(new Exception());
});

You could even extend this to do a Replay at the end of the lambda for all the mocks that were configured in it.

@henri-tremblay
Copy link
Contributor

Hi. Yes. This is the kind of things that are interesting to dig in. Not for the expectation directly but in a BDD style I think.

Easymock.given(() -> {
   expect(target.callThatThrows()).andThrow(new Exception());
}
EasyMock.when(() -> {
   testedCall.doSomething();
});
EasyMock.then(() -> {
});

Making the replay and verify implicit.

However, I don't have much bandwidth these days. If you would like to collaborate, that would be awesome.

Right now, if you want to implement your example, you can have your own EasyMockUtil class with

  public static <T> IExpectationSetters<T> expect(Callable<T> value) {
    try {
      return EasyMock.expect(value.call());
    } catch (Exception e) {
      throw new AssertionError(e); // will never ever happen
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants