-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create adapter for JUnit * Add support for Predicate in Junit adapter
- Loading branch information
1 parent
d7e5ad4
commit 11733f1
Showing
6 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
ext { | ||
name = 'Jazon JUnit Adapter' | ||
description = 'A library for test assertions on JSON payloads - for JUnit framework.' | ||
} | ||
|
||
apply from: '../gradle/publishing.gradle' | ||
|
||
dependencies { | ||
compile project(':jazon-core') | ||
testCompile group: 'junit', name: 'junit', version: '4.12' | ||
testCompile group: 'com.google.guava', name: 'guava', version: '27.1-jre' | ||
} |
33 changes: 33 additions & 0 deletions
33
jazon-junit/src/main/java/com/zendesk/jazon/junit/JazonJunitAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.zendesk.jazon.junit; | ||
|
||
import com.zendesk.jazon.MatchResult; | ||
import com.zendesk.jazon.MatcherFactory; | ||
import com.zendesk.jazon.actual.GsonActualFactory; | ||
|
||
import static com.zendesk.jazon.util.Preconditions.checkNotNull; | ||
|
||
public class JazonJunitAdapter { | ||
private static final GsonActualFactory GSON_ACTUAL_FACTORY = new GsonActualFactory(); | ||
private static final MatcherFactory matcherFactory = new MatcherFactory(); | ||
private final String actualJson; | ||
|
||
public JazonJunitAdapter(String actualJson) { | ||
this.actualJson = checkNotNull(actualJson); | ||
} | ||
|
||
public static JazonJunitAdapter assertThat(String actualJson) { | ||
return new JazonJunitAdapter(actualJson); | ||
} | ||
|
||
public void matches(Object expected) { | ||
MatchResult matchResult = matcherFactory.matcher() | ||
.expected(expected) | ||
.actual(GSON_ACTUAL_FACTORY.actual(actualJson)) | ||
.match(); | ||
if (matchResult.ok()) { | ||
return; | ||
} | ||
String mismatchMessageTemplate = "\n-----------------------------------\nJSON MISMATCH:\n%s\n-----------------------------------\n"; | ||
throw new AssertionError(String.format(mismatchMessageTemplate, matchResult.message())); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
jazon-junit/src/test/java/com/zendesk/jazon/junit/ExampleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.zendesk.jazon.junit; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Predicate; | ||
|
||
import static com.zendesk.jazon.junit.JazonJunitAdapter.assertThat; | ||
import static java.util.Arrays.asList; | ||
|
||
|
||
public class ExampleTest { | ||
|
||
@Test(expected = AssertionError.class) | ||
public void simpleTest() { | ||
// given | ||
String actualJson = "{\"value\": 123}"; | ||
Map<String, Object> expectedJsonAsMap = ImmutableMap.<String, Object>builder() | ||
.put("value", 50) | ||
.build(); | ||
|
||
// then | ||
assertThat(actualJson).matches(expectedJsonAsMap); | ||
} | ||
|
||
@Test(expected = AssertionError.class) | ||
public void testWithNestedArray() { | ||
// given | ||
String actualJson = "{" + | ||
"\"value\": 50," + | ||
"\"tags\": [\"blue\", \"black\", \"red\"]" + | ||
"}"; | ||
|
||
// then | ||
assertThat(actualJson).matches( | ||
deal(50, asList("blue", "pink", "red")) | ||
); | ||
} | ||
|
||
@Test(expected = AssertionError.class) | ||
public void testWithRootArray() { | ||
// given | ||
String actualJson = "[\"blue\", \"black\", \"red\"]"; | ||
|
||
// then | ||
assertThat(actualJson).matches(asList("blue", "pink", "red")); | ||
} | ||
|
||
@Test | ||
public void testRegex() { | ||
// given | ||
String actualJson = "[\"blue\", \"black\", \"red\"]"; | ||
|
||
// then | ||
assertThat(actualJson).matches( | ||
asList( | ||
regex("bl.*"), | ||
regex("bl.*"), | ||
regex("r.*") | ||
) | ||
); | ||
} | ||
|
||
private Predicate<String> regex(String reg) { | ||
return s -> s.matches(reg); | ||
} | ||
|
||
private static Map<String, Object> deal(int value, List<String> tags) { | ||
return ImmutableMap.<String, Object>builder() | ||
.put("value", value) | ||
.put("tags", tags) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
rootProject.name = 'jazon' | ||
include 'jazon-core' | ||
include 'jazon-spock' | ||
include 'jazon-junit' | ||
|