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.

Soft assertion chain broken by asInstanceOf() and extracting()

See original GitHub issue

Summary

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:

  1. Create the assertion using AssertionFactory.createAssert()
  2. Check to see if this is a soft assertion (this can be done by introspection to look for the errorCollector field added by the SoftProxies).
    1. If it is not, return the created assertion as normal.
    2. 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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
kriegfrjcommented, Mar 6, 2022

Sorry, this issue has been flushed from my cache 😄 - when I get back to this problem I’ll double-check.

1reaction
Bananeweizencommented, Dec 12, 2022

I guess this is basically the same bug pattern?

softly.assertThat(somePath)
.content() // breaks on first path not existing instead of continuing the soft assertions
.contains("foo");
Read more comments on GitHub >

github_iconTop 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 >

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