Let's try to improve the following:
- Our tests contain a lot of duplication
- Setting up data for our tests was long and painful
Test Data Builders are particularly handful to build test data. They should help us here.
You'll find resource about Test Data Builders here or on your room walls.
As a group, what are the top 5 most important things about the Test Data Builders?
To help you to get started, here is a demo of how to use the Test Data-Builders in our particular situation.
Code Snippets of NovelTestDataBuilder from the video
package com.murex.tbw.domain.book;
import com.google.common.collect.Lists;
import com.murex.tbw.domain.country.Language;
public class NovelTestDataBuilder {
private double price = 10;
public static NovelTestDataBuilder aNovel() {
return new NovelTestDataBuilder();
}
public NovelTestDataBuilder costing(double price) {
this.price = price;
return this;
}
public Novel build() {
return new Novel("Test Data Builders for Dummies", price, null, Language.ENGLISH, Lists.newArrayList());
}
}
Code Snippets of InvoiceTest from the video
package com.murex.tbw.purchase;
import com.murex.tbw.domain.book.NovelTestDataBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class InvoiceTest {
@Test
void Test_Data_Builders_Constraint_Applies_tax_rules_when_computing_total_amount() {
Invoice invoice = InvoiceTestDataBuilder.anInvoice()
.from(USA)
.with(PurchasedBookTestDataBuilder.aPurchasedBook()
.of(NovelTestDataBuilder.aNovel()
.costing(50.0)
.build())
.build())
.build();
Assertions.assertEquals(50 * 1.15 * 0.98, invoice.computeTotalAmount());
}
}
Now that you know everything about Test Data Builders, try to use them to improve your tests.
In real life, you would
- Fix the bugs
- Use test data-builders to instantiate your test data.
So let's Ignore or Comment-Out the previous tests!
We'll first focus on testing the Invoice (java | c++ | c# | kotlin | scala) class. It's simpler and achievable given the time we have.
To solve this section, use the test entitled Test_Data_Builders_Constraint_Applies_tax_rules_when_computing_total_amount!
Use the test data-builder to instantiate your objects in the test.
If you have the time, repeat the same exercise with the test on ReportGenerator (java | c++ | c# | kotlin | scala) class.
Time for a mini-retro again.
Take a few minutes to discuss the good and the bad of this approach.
Then compare them to what people usually say in the Retrospectives Guide
Continue: