Statement for BigCo
Hamlet: $650.00 (55 seats)
As You Like It: $580.00 (35 seats)
Othello: $500.00 (40 seats)
Amount owed is $1,730.00
You earned 47 credits
Approval tests : create a test
The verify is an approvaltests method that will compare the result returned by the print method and our Golden master.
StatementPrinterTests.java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.approvaltests.Approvals.verify;
public class StatementPrinterTests {
@Test
void exampleStatement() {
Map<String, Play> plays = Map.of(
"hamlet", new Play("Hamlet", "tragedy"),
"as-like", new Play("As You Like It", "comedy"),
"othello", new Play("Othello", "tragedy"));
Invoice invoice = new Invoice("BigCo", List.of(
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40)));
StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.print(invoice, plays);
verify(result);
}
Have we missed something ?
We should ask ourselves if we have covered every piece of code with our test.
To do so we have a tool : code coverage.
The Code Coverage (from IntelliJ here) shows us that the default case is not covered at the moment.
So let's add a new test :
StatementPrinterTests.java
@Test
void statementWithNewPlayTypes() {
Map<String, Play> plays = Map.of(
"henry-v", new Play("Henry V", "history"),
"as-like", new Play("As You Like It", "pastoral"));
Invoice invoice = new Invoice("BigCo", List.of(
new Performance("henry-v", 53),
new Performance("as-like", 55)));
StatementPrinter statementPrinter = new StatementPrinter();
Assertions.assertThrows(Error.class, () -> {
statementPrinter.print(invoice, plays);
});
}
Are we confident enough in our tests ?
To check this we can check the quality of our tests by using a concept called mutation testing.
You can use tools like pitest (for Java) or stryker (for C#, Javascript, Scala).
Now we are confident enough, let's do the exercise.