Giter Site home page Giter Site logo

Comments (3)

scordio avatar scordio commented on July 24, 2024

Could you provide a real-life example you'd like to see improved?

A similar request has been made at #1652 and #3262, but I wonder if yours is 100% a duplicate.

from assertj.

joel-costigliola avatar joel-costigliola commented on July 24, 2024

I want to understand a bit more the problem you are trying to solve @goatfryed.

You are talking about asserting on X and then want to get Y out of X and make further assertions on Y.
I would typically write something like:

// step 1
X x = computeX();
assertThat(x).isNotNull();
Name name = x.getName(); // Y is Name here
assertThat(name).startsWith("Foo");
// step 2
Stuff s = computeStuff(name);
// alternative: call x.getName() twice and remove name local variable
assertThat(s).isNotNull(); 

Is there something that prevents you to do that ? Maybe there is no getName() and you would need to use introspection to get it ? I don't think it is that ugly (but that's just my opinion)

If I understand you correctly, you would prefer code like:

X x = computeX();
Name name = assertThat(computeX()).isNotNull()
                                  .extracting(X::getName) // or extracting("name") by reflection
                                  .startsWith("Foo")
                                  .unwrap();
Stuff s = computeStuff(n); 
assertThat(s).isNotNull(); 

Note this other alternative:

X x = computeX();
Name name = assertThat(computeX()).isNotNull()
                                  .extracting(X::getName) // or extracting("name") by reflection
                                  .startsWith("Foo")
                                  .extracting(n -> computeStuff(n))
                                  .isNotNull(); 

from assertj.

goatfryed avatar goatfryed commented on July 24, 2024

I've updated the description. The initial example was indeed quite abstract.

I find this helpful mostly in combination with deeper nested list properties. Especially in combination with thinks like filteredOn.

It's not difficult to access the nested value for further processing, but it feels redundant to traverse the object structure once to assert and then a second time to continue processing.

My use case is mostly in integration test calling various apis. Here, the object tree tends to get large. It's not even mostly about filtering. Some of my use cases include paths like order.items[0].product.relations[0].product.places[0].address. So even if the access chain is straightforward, it feels redundant and makes the test harder to read, because the reader needs to understand that i'm accessing the previously asserted element with my call chain.

from assertj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.