lukas-krecan / JsonUnit

Compare JSON in your Unit Tests
Apache License 2.0
893 stars 115 forks source link

[Feature] Add softly assertion support #145

Open rkarczmarczyk opened 6 years ago

rkarczmarczyk commented 6 years ago

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;
    }
    }
  2. 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);
    }
    }
  3. 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);
    }
    }
lukas-krecan commented 5 years ago

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

javydreamercsw commented 5 years ago

Looks like the issue listed above has been merged.

lukas-krecan commented 4 years ago

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

lukas-krecan commented 4 years ago

Maybe the new AssertJ features like SoftAssertionsProvider might be the way

typowy1 commented 2 years ago

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

lukas-krecan commented 2 years ago

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

typowy1 commented 2 years ago

Ok, thanks for answer.

akshayamaldhure commented 2 years ago

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