public class Approver extends Object
# Approvals
Approval Testing is a way of considering Unit Tests without focusing on writing tons of assertions, but by letting the framework generate the references to use in assertions.
Approvals is this framework’s main entry point for validation of data produced by your program, by comparing it to data you already reviewed and approved manually.
Using Approvals for checking your program’s data will actually trigger a comparison between the output it produces, and content stored in a file somewhere in your project’s tests. If that file doesn’t exist, the framework will warn you and propose you to review the data produced by your program to approve (or not) what has been generated. After you reviewed it and selected the proper output, it’ll use the resulting file for future comparisons.
An Approvals object can be constructed with:
a Class to be linked with the object. It is used in order to automatically compute the path and name of the approved files, used to store the output data of your program.
a Reporter, to be used if differences are found between your program’s output and the approved file’s content. Some Reporters are provided by the framework, but you can also provide your own.
The basic and main entry point you should have a look at in this Approvals class is the verify(Object) method, allowing to compute a comparison between an output and some content stored in an approved file.
Reporter| Constructor and Description |
|---|
Approver()
Standard approvals, with default
Reporter and file name detected from the test class used to apply the constructor and the test method used to apply a verify(Object) method. |
| Modifier and Type | Method and Description |
|---|---|
Approver |
namedArguments(String... names)
Specifies a header naming the arguments of the function under test.
|
Approver |
reportTo(Reporter reporter)
Specifies the reporter used to report mismatches.
|
Approver |
testing(Class<?> testClass)
Specifies the testClass to use as a folder name to store approved and received files.
|
void |
verify(Object output)
Compares the actual output of your program (the function’s argument) and the content of the approved file matching with the test method.
|
void |
verify(Path output)
Compares the actual output of your program (the function’s argument) and the content of the approved file matching with the test method.
|
Approver |
writeTo(String customFileName)
Specifies the name to use for approved and received files.
|
Approver |
writeToFolder(String folder)
Specifies the folder name to store approved and received files.
|
public Approver()
Standard approvals, with default Reporter and file name detected from the test class used to apply the constructor and the test method used to apply a verify(Object) method.
public Approver reportTo(Reporter reporter)
Specifies the reporter used to report mismatches.
public Approver writeTo(String customFileName)
Specifies the name to use for approved and received files.
public Approver testing(Class<?> testClass)
Specifies the testClass to use as a folder name to store approved and received files.
public Approver writeToFolder(String folder)
Specifies the folder name to store approved and received files.
public Approver namedArguments(String... names)
Specifies a header naming the arguments of the function under test. This header prefixes the approved and received files.
names - one name for each argumentpublic void verify(Object output)
Compares the actual output of your program (the function’s argument) and the content of the approved file matching with the test method.
It’ll use a temporary received file to store the output of your program. This file will be erased in case the results are matching. Otherwise, it will be kept for you to review it.
In case of differences found in the output, the Reporter linked to this Approvals instance will be called (Reporter.mismatch(ApprovalFiles) ).
output - Any object with a Object.toString() representation containing the output of your program. It will be compared to the associated approved file.AssertionError - if the Reporter implementation relies on standard assertions provided by a framework like JUnitRuntimeException - if the Reporter relies on executing an external command which failedpublic void verify(Path output)
Compares the actual output of your program (the function’s argument) and the content of the approved file matching with the test method.
It’ll use a temporary received folder to store a copy of the output of your program. This file will be erased in case the results are matching. Otherwise, it will be kept for you to review it.
In case of differences found in the output, the Reporter linked to this Approvals instance will be called (Reporter.mismatch(ApprovalFiles) ).
output - a Path containing the output of your program. It will be compared to the associated approved file.AssertionError - if the Reporter implementation relies on standard assertions provided by a framework like JUnitRuntimeException - if the Reporter relies on executing an external command which failedCopyright © 2018–2019 Write Them First!. All rights reserved.