One method is to create a custom verify()
method tailored to your specific needs.
For example, here's how to create a custom method for handling JSON:
public static void verifyAsJson(Object o, Options options)
{
Approvals.verify(JsonUtils.asJson(o), options.forFile().withExtension(".json"));
}
Another option is to create an object that knows how to verify itself. To do this, you'll need to implement the required interfaces:
And then just call verify(yourVerifiableObject)
If verify is called with an instance of Verifiable
it will do a callback, allowing you to do whatever is needed,
for example:
@Test
void testVerifiable()
{
verify(new MarkdownParagraph("Paragraph Title", "This is where the paragraph text is."));
}
public static class MarkdownParagraph implements Verifiable
{
private String title;
private String paragraph;
public MarkdownParagraph(String title, String paragraph)
{
this.title = title;
this.paragraph = paragraph;
}
@Override
public VerifyParameters getVerifyParameters(Options options)
{
return new VerifyParameters(options.forFile().withExtension(".md"));
}
@Override
public String toString()
{
return String.format("# %s\n%s", title, paragraph);
}
}