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

Overload ResultMatchers.failureCountIs: take a Matcher of integer #1302

Conversation

alb-i986
Copy link
Contributor

This allows clients to assert that the failure count is greaterThan, lessThan, etc.

This allows clients to assert that the failure count is greaterThan, lessThan, etc.

@Test
public void failureCountIsTakingMatcherShouldHaveGoodDescription() {
Matcher<Integer> matcherTestStub = new BaseMatcher<Integer>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a Matcher<Number> to ensure that the code compiles?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong?
Compilation looks fine to me:
https://travis-ci.org/junit-team/junit4/builds/131156153

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing is wrong but nothing verifies that you can use this new API with Matcher<Number> or Matcher<Object>

Copy link
Contributor Author

@alb-i986 alb-i986 May 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean.

I understand this may be a bit OT but if we take one step back, are we sure we need a lower bound?

failureCountIs(Matcher<? super Integer>)

I think we need quite the opposite: an upper bound, so as to allow any Matcher<Integer> and below, more specific; we do not want to allow something generic like Matcher<Object>, do we?

See also https://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html:

An "in" variable is defined with an upper bounded wildcard, using the extends keyword.

But since Integer is final, we may just go for no bound at all: Matcher<Integer> or at most Matcher<? extends Comparable<Integer>>, which happens to be the return type of greaterThan.

    ResultMatchers.failureCountIs(allOf(equalTo(2), greaterThan(2)));

The above line compiles with both Matcher<Integer> and Matcher<? extends Comparable<Integer>>.

Then, again, I understand that Matcher<? super Integer> is what hamcrest uses in cases like this (e.g. in arrayWithSize) , so I'll understand if you prefer to comply.

Copy link
Member

@kcooney kcooney May 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: I am sure you do not want an upper bound. Note that hasSize() uses Matcher<? super Integer> (Javadoc)

If the thing you were matching was a Car, You would want a compile error if someone passed in a Matcher<Tesla> because Matcher<Testla> allows you to express constraints that would never pass for a Edsel (ex: expectations on the battery size). But you would want to support passing in Matcher<Vehicle> (for "has engine" and "has passenger capacity) or ``Matcher` (for "is not null").

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I absolutely agree with @kcooney.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alb-i986 Can the type of matcherTestStub be changed to Matcher<Number>?

Otherwise this LGTM

@kcooney
Copy link
Member

kcooney commented May 29, 2016

@marcphilipp at one point we talked about reducing dependencies from JUnit to Hamcrest. Is that still the plan, or did plans change with JUnit Lambda?

@marcphilipp
Copy link
Member

As far as I remember that was at a time when we thought we would evolve the JUnit 4 codebase into JUnit 5. Since that plan has become obsolete I don't see a reason why we shouldn't improve existing Hamcrest-based APIs.

@kcooney
Copy link
Member

kcooney commented May 29, 2016

@marcphilipp Does that mean we should remove the deprecations we did in #1150 ?

@marcphilipp
Copy link
Member

On second thought, I don't think we should add additional APIs that rely on Hamcrest. I'm not sure about Hamcrest's progress towards a new version, but IMHO since `ResultMatchers´ should never have been a public API in the first place, I think we should leave it as is.

@kcooney
Copy link
Member

kcooney commented May 31, 2016

Closing

Thanks for the contribution, @alb-i986; sorry we couldn't merge it

@kcooney kcooney closed this May 31, 2016
@alb-i986 alb-i986 deleted the overload-ResultMatchers-failureCountIs branch June 23, 2016 20:03
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

Successfully merging this pull request may close these issues.

None yet

3 participants