JavaHamcrest icon indicating copy to clipboard operation
JavaHamcrest copied to clipboard

All matchers should implement equals and hashCode methods.

Open PawelAdamski opened this issue 8 years ago • 3 comments

Equals and hashCode methods are in general useless when one uses Hamcrest for testing. But I want to build a library for mocking Apache HttpClient and I plan to build it on top of Hamcrest Matchers. I want to test if my code generates correct set of matchers. Now I need to write: assertTrue(urlConditions.hostConditions.get(0).matches("localhost")); but I would prefer to write assertThat(urlConditions.hostConditions,contains(Matchers.equalTo("localhost"))); to do so Matchers would have to implement equals and hashCode methods.

PawelAdamski avatar Oct 13 '17 08:10 PawelAdamski

This would be a big change for an unusual requirement. Can you provide a bit more context for what you think your tests would look like?

sf105 avatar Oct 13 '17 08:10 sf105

Why do you think it is a big change? I see it rather small, equals and hashcode are very simple and can be even generated automatically.

I'm writing a library for mocking Apache HttpClient. It will work similarly to WireMock. A user will be able to write:

httpClient.onPost("http://ww.example.com") .withParameter("foo",equalsTo("abc")) .withRefrence(emptyString()) .doReturn("Example");

Suche a code will generate a set of Matchers. Later when someone executes HTTP request using that client and it matches all Matchers it returns response "Example".

Part of working on the library is testing. I want to check if a generated set of Matchers contains expected ones. Is this explanation enough? If you don't mind I could try to implement them on my own.

PawelAdamski avatar Oct 13 '17 12:10 PawelAdamski

The effort would be that we would have to enforce it across all the Matchers we've implemented, which is quite a lot. If it was part of the protocol, then other people implementing Matchers would also have to add them. Question, are you generating new Matchers, or just combinations of existing matchers?

sf105 avatar Jun 01 '18 16:06 sf105