question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement extracting/contains assertions

See original GitHub issue

AssertJ has really powerful extracting/contains mechanism. See: http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html Section: Assertions on extracted properties/fields of iterable/array elements

I think it worth to implement similar/same for fluent part of the library. I think we can just add assertJ core as a dependency and implement extracting method which returns: “AbstractListAssert” and we’ll just leverage assertions they have. In the end we’ll have something like: JsonFluentAssert.assertThatJson(jsonResponse.getBody().getObject()) .node("data").isPresent().isArray().ofLength(3) .extracting(“name”, “age”, “race.name”) .contains(tuple(“Boromir”, 37, “Man”), tuple(“Sam”, 38, “Hobbit”), tuple(“Legolas”, 1000, “Elf”));

What do you think? If you are agree and like the idea, I can work on it.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:22 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
lukas-krecancommented, Feb 14, 2018

I have started to play with it see https://github.com/lukas-krecan/JsonUnit/pull/102

0reactions
eximius313commented, Sep 19, 2018

It works! Although in perfect world this:

myAssertion.and(
    a -> a.node("name").isString().isEqualTo(name), 
    a -> a.node("street").isString().isEqualTo(street)
);

could coexist together with this:

myAssertion.
    .node("name", a -> a.isString().isEqualTo(name))
    .node("street", a -> a.isString().isEqualTo(street))

but having just one is 100 times better than nothing 😉

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

AssertJ / Fluent assertions for java
Note that extracting one property can be made strongly typed by giving the property type as the second argument. // to have type...
Read more >
Asserting Lists with AssertJ - Reflectoring
How can we extract certain elements out of a list to assert them? This article shows how to work with lists in AssertJ....
Read more >
how to use assertj extracting map property - Stack Overflow
AssertJ has entry() method. You can assert map value like this. assertThat(list) .extracting("myMap") .contains(entry("foo1", "bar1"), entry("foo2", ...
Read more >
Testing Java Collections with AssertJ. - Medium
You can either use method reference or // lambda expression in "extracting". // "contains" is using for checking if there are specific names...
Read more >
A new way of extracting values in AssertJ collections assertions
Now, when you wanted to make some assertions on the Person instances, you had to extract values using extracting method. An example would...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found