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

HasPropertyWithValue #242

Closed
EarthCitizen opened this issue Sep 21, 2014 · 7 comments
Closed

HasPropertyWithValue #242

EarthCitizen opened this issue Sep 21, 2014 · 7 comments

Comments

@EarthCitizen
Copy link

I have looked and not found it in the API. I could be overlooking it, but it would be great to be able to do the following:

public class Foo {
    int amount;
    String name;
    List<String> items;

    public int getAmount() {...}

    public String getName() {...}

    public List<String> getItems() {...}
}
Foo foo = new Foo();
assertThat(foo).hasPropertyWithValue("amount", 5).hasPropertyWithValue("name", "Someone");
@joel-costigliola
Copy link
Member

No such assertion indeed.

In that case, I would rather use:

assertThat(foo.getAmount()).isEqualTo(5)
assertThat(foo.getName()).isEqualTo("Someone");

or even better, I would write a custom assertion class for Foo

assertThat(foo).hasAmount(5).hasName("Someone");

If you are too lazy like me to write your own assertion classes, just use the assertion generator.

@EarthCitizen
Copy link
Author

OK. If I get the time, maybe I can make a pull request 😄

@joel-costigliola
Copy link
Member

Considering the alternatives I have suggested, I'm not convinced by the usefulness of such assertions.
Before doing any work, try to convince me that there is a use case for your assertion, I don't want you to waste your time !

@EarthCitizen
Copy link
Author

It just gives another option. Maybe not all cases warrant a custom assertion class; like if you are only using an object in one test. I do agree though that if you are going to be doing a significant amount of tests around a specific class, you are better off with a custom assertion class.

Also, originally I was thinking that all of the chained conditions would be checked on failure. But is that not the case? If hasAmount(5) fails, will hasName("foo") still be checked?

If only the first failure is reported, then it would just be easier to write multiple asserts as in your first example.

@joel-costigliola
Copy link
Member

By default, only the first failure is reported whether you chain or not your assertions.
If you want to have all the errors reported, use SoftAssertions or JUnitSoftAssertions.
cf : http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#soft-assertions

@PascalSchumacher
Copy link
Member

I guess this can be closed, as 77ab461 added #hasFieldOrPropertyWithValue

@joel-costigliola
Copy link
Member

Indeed !

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

3 participants