JsonUnit icon indicating copy to clipboard operation
JsonUnit copied to clipboard

[Feature] Add softly assertion support

Open rkarczmarczyk opened this issue 7 years ago • 8 comments

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 avatar Oct 31 '18 23:10 rkarczmarczyk

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

lukas-krecan avatar Dec 08 '18 08:12 lukas-krecan

Looks like the issue listed above has been merged.

javydreamercsw avatar Jan 09 '19 20:01 javydreamercsw

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

lukas-krecan avatar May 18 '20 18:05 lukas-krecan

Maybe the new AssertJ features like SoftAssertionsProvider might be the way

lukas-krecan avatar Jun 18 '20 05:06 lukas-krecan

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

typowy1 avatar Feb 12 '22 08:02 typowy1

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

lukas-krecan avatar Feb 17 '22 18:02 lukas-krecan

Ok, thanks for answer.

typowy1 avatar Mar 02 '22 16:03 typowy1

@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.

akshayamaldhure avatar Mar 07 '22 07:03 akshayamaldhure