Tutorial - dakusui/thincrest GitHub Wiki
Suppose that you have a class to be tested like this. (Constructors and getters are omitted. Full version is found here)
public static class BankAccount {
public static class Record {
public enum Type {
WITHDRAW,
DEPOSIT
}
private final Type type;
private final int amount;
...
@Override
public String toString() {
return String.format("%s:%s", this.getType(), this.getAmount());
}
}
final String name;
int balance;
List<Record> history = new LinkedList<>();
...
public void deposit(int amount) {
this.balance += amount;
this.history.add(new Record(Record.Type.WITHDRAW /* BUG! */, amount));
}
public void withdraw(int amount) {
this.balance -= amount;
this.history.add(new Record(Record.Type.WITHDRAW, amount));
}
@Override
public String toString() {
return "BankAcccount:" + this.name;
}
}
And your test scenario looks like this.
BankAccount bankAccount = new BankAccount("John Doe");
bankAccount.deposit(1000);
bankAccount.withdraw(110);
Once you install the dependency, lets static import thincrest's facade Crest
to use it from your test class.
import static com.github.dakusui.crest.Crest.*;
This enables you to use following static methods without typing the class name.
- assertThat(...)
- allOf(Matcher<? super T>... matchers)
- anyOf(Matcher<? super T>... matchers)
- asObject(...)
- asComparableOf(...)
- asString(...)
- asObjectList(...)
- asListOf(...)
- asObjectMap(...)
- asMapOf(...)
The assertion you would write will be like this.
assertThat(
bankAccount,
allOf(
asString("getName").equalTo("John Doe").$(),
asInteger("getBalance").equalTo(890).$(),
asObject(call("getHistory").andThen("get", 0).andThen("getType").$())
.equalTo(BankAccount.Record.Type.DEPOSIT).$()
)
);