diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-27 17:00:25 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-27 17:01:24 +0200 |
commit | 73a38b94539a3f69c084c210cb2df9287d2c00e2 (patch) | |
tree | bfcfbcbc3edadfda8365c93e3e16adf9e6d407af /sonar-testing-harness/src/test | |
parent | aba9715aa0e4fcf433f3f7639098823e95db8c83 (diff) | |
download | sonarqube-73a38b94539a3f69c084c210cb2df9287d2c00e2.tar.gz sonarqube-73a38b94539a3f69c084c210cb2df9287d2c00e2.zip |
JsonAssert can ignore fields and small improvements
Diffstat (limited to 'sonar-testing-harness/src/test')
-rw-r--r-- | sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java | 56 | ||||
-rw-r--r-- | sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java | 6 |
2 files changed, 31 insertions, 31 deletions
diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java index de21bc3068d..85d38aa816c 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/JsonAssertTest.java @@ -19,11 +19,12 @@ */ package org.sonar.test; -import org.junit.ComparisonFailure; -import org.junit.Test; - import java.io.File; import java.net.URL; +import org.junit.ComparisonFailure; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -31,6 +32,9 @@ import static org.sonar.test.JsonAssert.assertJson; public class JsonAssertTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Test public void isSimilarAs_strings() { assertJson("{}").isSimilarTo("{}"); @@ -50,48 +54,44 @@ public class JsonAssertTest { URL url1 = getClass().getResource("JsonAssertTest/sample1.json"); URL url2 = getClass().getResource("JsonAssertTest/sample2.json"); assertJson(url1).isSimilarTo(url1); + expectedException.expect(AssertionError.class); - try { - assertJson(url1).isSimilarTo(url2); - fail(); - } catch (AssertionError error) { - // ok - } + assertJson(url1).isSimilarTo(url2); } @Test public void actual_can_be_superset_of_expected() { assertJson("{\"foo\": \"bar\"}").isSimilarTo("{}"); - try { - assertJson("{}").isSimilarTo("{\"foo\": \"bar\"}"); - fail(); - } catch (AssertionError error) { - // ok - } + expectedException.expect(AssertionError.class); + + assertJson("{}").isSimilarTo("{\"foo\": \"bar\"}"); } - @Test(expected = IllegalStateException.class) + @Test public void fail_to_load_url() throws Exception { + expectedException.expect(IllegalStateException.class); + assertJson(new File("target/missing").toURI().toURL()); } @Test public void enable_strict_order_of_arrays() { - try { - assertJson("[1,2]").setStrictArrayOrder(true).isSimilarTo("[2, 1]"); - fail(); - } catch (AssertionError error) { - // ok - } + expectedException.expect(AssertionError.class); + + assertJson("[1,2]").withStrictArrayOrder().isSimilarTo("[2, 1]"); } @Test public void enable_strict_timezone() { - try { - assertJson("[\"2010-05-18T15:50:45+0100\"]").setStrictTimezone(true).isSimilarTo("[\"2010-05-18T16:50:45+0200\"]"); - fail(); - } catch (AssertionError error) { - // ok - } + expectedException.expect(AssertionError.class); + + assertJson("[\"2010-05-18T15:50:45+0100\"]").withStrictTimezone().isSimilarTo("[\"2010-05-18T16:50:45+0200\"]"); + } + + @Test + public void ignore_fields() { + assertJson("{\"foo\": \"bar\"}") + .ignoreFields("ignore-me") + .isSimilarTo("{\"foo\": \"bar\", \"ignore-me\": \"value\"}"); } } diff --git a/sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java b/sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java index d640a76367d..d52fe865e67 100644 --- a/sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java +++ b/sonar-testing-harness/src/test/java/org/sonar/test/JsonComparisonTest.java @@ -137,7 +137,7 @@ public class JsonComparisonTest { @Test public void strict_timezone() { - assertThat(new JsonComparison().setStrictTimezone(true).isStrictTimezone()).isTrue(); + assertThat(new JsonComparison().withTimezone().isStrictTimezone()).isTrue(); // same instant, same timezone assertThat(isSimilar_strict_timezone("{\"foo\": \"2010-05-18T15:50:45+0100\"}", "{\"foo\": \"2010-05-18T15:50:45+0100\"}")).isTrue(); @@ -169,10 +169,10 @@ public class JsonComparisonTest { } private boolean isSimilar_strict_timezone(String expected, String actual) { - return new JsonComparison().setStrictTimezone(true).areSimilar(expected, actual); + return new JsonComparison().withTimezone().areSimilar(expected, actual); } private boolean isSimilar_strict_array_order(String expected, String actual) { - return new JsonComparison().setStrictArrayOrder(true).areSimilar(expected, actual); + return new JsonComparison().withStrictArrayOrder().areSimilar(expected, actual); } } |