-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
F!! added support for using jsonapprovals with jackson
- Loading branch information
1 parent
08f336e
commit 462a641
Showing
9 changed files
with
101 additions
and
7 deletions.
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
6 changes: 0 additions & 6 deletions
6
approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.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
40 changes: 40 additions & 0 deletions
40
approvaltests-tests/src/test/java/org/approvaltests/JsonJacksonApprovalsTest.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,40 @@ | ||
package org.approvaltests; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class JsonJacksonApprovalsTest | ||
{ | ||
@Test | ||
void testDuplicateFields() | ||
{ | ||
JsonJacksonApprovals.verifyAsJson(new MyOtherClass()); | ||
} | ||
private static class MyClass | ||
{ | ||
@JsonIgnore | ||
private String name = "MyClass"; | ||
public String lastName = "MyClass"; | ||
public String getName() | ||
{ | ||
return name; | ||
} | ||
public void setName(String name) | ||
{ | ||
this.name = name; | ||
} | ||
} | ||
private static class MyOtherClass extends MyClass | ||
{ | ||
@JsonIgnore | ||
private String name = "MyOtherClass"; | ||
public String getName() | ||
{ | ||
return name; | ||
} | ||
public void setName(String name) | ||
{ | ||
this.name = name; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...rc/test/java/org/approvaltests/JsonJacksonApprovalsTest.testDuplicateFields.approved.json
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,3 @@ | ||
{ | ||
"lastName" : "MyClass" | ||
} |
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
30 changes: 30 additions & 0 deletions
30
approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.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,30 @@ | ||
package org.approvaltests; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.spun.util.ObjectUtils; | ||
import org.approvaltests.core.Options; | ||
|
||
public class JsonJacksonApprovals | ||
{ | ||
public static void verifyAsJson(Object o) | ||
{ | ||
verifyAsJson(o, new Options()); | ||
} | ||
public static void verifyAsJson(Object o, Options options) | ||
{ | ||
Approvals.verify(asJson(o), options.forFile().withExtension(".json")); | ||
} | ||
public static String asJson(Object o) | ||
{ | ||
try | ||
{ | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(o); | ||
} | ||
catch (JsonProcessingException e) | ||
{ | ||
throw ObjectUtils.throwAsError(e); | ||
} | ||
} | ||
} |