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

Allow "normal" objects to describe themself, e.g. in case of a mismatch #244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PirolA
Copy link

@PirolA PirolA commented Jan 4, 2019

Motivation
I needed to give detailed descriptions of an object in case of a mismatch and couldn't use toString().

Solution

  • Use the existing SelfDescribing Interface and retrieve a description based on its describeTo method.

@sf105
Copy link
Member

sf105 commented Jan 4, 2019

What is the situation where you can't use the appendDescriptionOf method?

@PirolA
Copy link
Author

PirolA commented Jan 9, 2019

If I remember correctly, this only works for matchers, but not for POJOs, right?

Roughly speaking, my problem occured when calling assertThat(somePOJO, equalsTo(anotherPOJO)) and equalsTo() is using some information, that's not exposed by the toString() method. Then - in case of a mismatch - an insufficient error message is printed. As a "workaround" for this I used the mechanism in this PR.

@tumbarumba
Copy link
Member

This change seems like an improvement. @PirolA, can you add a test for the changed functionality to this PR?

@@ -29,6 +31,19 @@ public Description appendDescriptionOf(SelfDescribing value) {
public Description appendValue(Object value) {
if (value == null) {
append("null");
} else if (value instanceof SelfDescribing){

Choose a reason for hiding this comment

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

There is a fundamental problem with this approach: normally hamcrest would be a test dependency.

@nhojpatrick
Copy link
Member

@PirolA interesting idea, as some previous comments have highlights it would mean hamcrest being a non compile dependency. maybe looking at how spotbugsfindbugs have spotbugs-annotations/findbugs-annotations, where we could support something like thats a making a lightweight runtime jar if you really needed.

please can you rebase from master, as hamcrest-core and hamcrest-library have been refactored a lot and also deprecated, so that everything is just in hamcrest.

@nhojpatrick
Copy link
Member

Going to try and kick start hamcrest, so if you want to get it merged, please rebase from the branch v2.3-candidates.
Still trying to understand how has permissions to perform a release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Needs triage
Development

Successfully merging this pull request may close these issues.

5 participants