An Approval Testing library for Java and JUnit - like Llewellyn Falco's but more Java'y.
A helping hand for many testing problems.
If you are upgrading from verions 1.x to version 2.x - JUnit 4 support has been moved from com.oneeyedmen.junit
to com.oneeyedmen.junit4
.
In return, you no longer need to specify whether your are using Java or Kotlin
public class ApprovalsExtensionTest {
// Initialise okey-doke.
@RegisterExtension ApprovalsExtension approvals = new ApprovalsExtension();
// See other constructors to change where the files are stored,
// or change the extension
@Test
public void something_that_we_want_to_be_the_same_next_time(
Approver approver // approver will be injected
) {
Object result = doSomeCalculation(42, "banana");
approver.assertApproved(result); // check that the result is as approved
}
}
The first time you run this test it will fail, but the result of doSomeCalculation
will be written into a
file next to the test, named ApprovalsExtensionTest.something_that_we_want_to_be_the_same_next_time.actual
You can look at this file to check that it is what you expect, and if it is, approve the test by renaming the file
it to ApprovalsExtensionTest.something_that_we_want_to_be_the_same_next_time.approved
(or ask the plugin to do it for you).
From then on the test will pass provided the result of doSomeCalculation
doesn't change.
If it does change then you can either fix the code if it shouldn't have, or approve the new version.
There is an IntelliJ plugin (thanks @s4nchez) to help approve your output.
We still support JUnit 4 with a Rule - ApprovalsRuleTest
@Rule public final ApprovalsRule approver = ApprovalsRule.usualRule();
@Test
public void something_that_we_want_to_be_the_same_next_time(
) {
Object result = doSomeCalculation(42, "banana");
approver.assertApproved(result); // check that the result is as approved
}
Here you can use the ApprovalsRule
as a approver.