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

[Feature] Add softly assertion support #145

Open
rkarczmarczyk opened this issue Oct 31, 2018 · 8 comments
Open

[Feature] Add softly assertion support #145

rkarczmarczyk opened this issue Oct 31, 2018 · 8 comments

Comments

@rkarczmarczyk
Copy link

At this moment there is no easy way to collect Json asserts in bundle and evaluate it at the end of tests (to show all errors at one test iteration).

There is a way to create custom softly assert but it's not very convenient.
You have to do 3 steps:

  1. Create class that extends AbstractAssert
class JsonAssert extends AbstractAssert<JsonAssert, String> {
    JsonAssert(String actual) {
        super(actual, JsonAssert.class);
    }
    public static JsonAssert assertThat(String actual) {
        return new JsonAssert(actual);
    }

    JsonAssert isEqualTo(String expected) {
        assertThatJson(actual).isEqualTo(expected);
        return this;
    }
}
  1. Create proxy to already created class in class that extends JUnitSoftAssertions
public class CustomJUnitSoftAssertions extends JUnitSoftAssertions {
    public JsonAssert assertThatJson(String actual) {
        return proxy(JsonAssert.class, String.class, actual);
    }
}
  1. Used new softly assert in test
public class SoftlyTest {
    @Rule
    public final CustomJUnitSoftAssertions softly = new CustomJUnitSoftAssertions();
    @Test
    public void test() {
        String actualJson = "{}";
        String expectedJson = "{}";
        softly.assertThatJson(actualJson).isEqualTo(expectedJson);
    }
}
@rkarczmarczyk rkarczmarczyk changed the title Add softly assertion support [Feature] Add softly assertion support Oct 31, 2018
@lukas-krecan
Copy link
Owner

Waiting for release of this fix in AssertJ https://github.com/joel-costigliola/assertj-core/pull/1352/files

@javydreamercsw
Copy link

Looks like the issue listed above has been merged.

@lukas-krecan
Copy link
Owner

It can not be easily implemented since AssertJ softly proxy does not proxy all our methods like node()

@lukas-krecan
Copy link
Owner

Maybe the new AssertJ features like SoftAssertionsProvider might be the way

@typowy1
Copy link

typowy1 commented Feb 12, 2022

Hello, great library, is there any way to use it with softly assertions?

@lukas-krecan
Copy link
Owner

Hi, unfortunatelly I do not have capacity to implement it. But if you want to try, I welcome PRs.

@typowy1
Copy link

typowy1 commented Mar 2, 2022

Ok, thanks for answer.

@akshayamaldhure
Copy link

akshayamaldhure commented Mar 7, 2022

@rkarczmarczyk Do you happen to know how to make the workaround for an assertion like this?

            assertThatJson(stringArray1)
                    .when(Option.IGNORING_ARRAY_ORDER)
                    .whenIgnoringPaths("[*].key1", "[*].key2")
                    .isArray()
                    .isEqualTo(stringArray2);

EDIT: I figured this out.

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

5 participants