Soft assertion chain broken by asInstanceOf() and extracting()
See original GitHub issueSummary
The bytecode manipulation magic that makes soft assertions work doesn’t work with custom assertion factories, such as those used by extracting()
, asInstanceOf()
, etc.
I think this is related to #2495 and could be tackled at the same time.
Example
Object o = "string";
softly.assertThat(o)
.asInstanceOf(InstanceOfAssertFactories.STRING)
.contains("a")
.endsWith("b");
The call to asInstanceOf()
breaks the soft chain. So if the contains()
assert fails, it will throw an AssertionFailedError
, the rest of the test method won’t run (including endWith()
), and any soft assertions collected won’t be reported.
Solution
asInstanceOf()
and methods like it should:
- Create the assertion using
AssertionFactory.createAssert()
- Check to see if
this
is a soft assertion (this can be done by introspection to look for theerrorCollector
field added by theSoftProxies
).- If it is not, return the created assertion as normal.
- If it is, then generated a soft proxied copy of the created assertion using
this.errorCollector
as the error collector and return this instead of the raw created assertion.
A protected
method in AbstractAssert
should encapsulate this functionality so that custom assertions can leverage it too (as much of the necessary machinery is tucked away in package-private fields).
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Soft assertion chain broken by asInstanceOf() and extracting()
Solution. asInstanceOf() and methods like it should: Create the assertion using AssertionFactory.createAssert(); Check to see if ...
Read more >AssertJ - fluent assertions java library - GitHub Pages
Describing an assertion must be done before calling the assertion otherwise it is ignored as a failing assertion breaks will prevent the call...
Read more >AbstractAssert (AssertJ fluent assertions 3.18.1 API) - javadoc.io
Returns a String assertion for the toString() of the actual value, to allow chaining of String-specific assertions from this call.
Read more >AssertJ - fluent assertions java library - WebLab
Globally sets whether the AssertJ extracting capability considers bare-named property methods like String name(). Defaults to true.
Read more >AsserJ - chaing together returns and extracting - Stack Overflow
I wanted and found out how I can chain assertions one to another, but theres a case where I cannot figure out how...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry, this issue has been flushed from my cache 😄 - when I get back to this problem I’ll double-check.
I guess this is basically the same bug pattern?