Java Bean Equals Map

WebTau provides matchers/universal-compare universal comparison handlers to streamline Java Beans and Java Records assertions.Use map and bean comparison to validate individual java bean properties. Groovy def account = new Account("ac1", "My Account", "test account", new Address("TestingCity", "88888888")) account.should == [ id: "ac1", name: "My Second Account", address: [zipCode: "7777777"]] // only specified properties will be compared Check matchers/import-and-dependencies Import And Dependencies for prerequisites. Java Address address = new Address("TestingCity", "88888888"); Account account = new Account("ac1", "My Account", "test account", address); actual(account).should(equal(map( // utility function from WebTauCore static import "id", "ac1", "name", "My Second Account", "address", map("zipCode", "7777777")))); // only specified properties will be compared Check matchers/import-and-dependencies Import And Dependencies for prerequisites. public class Account { private final String id; private final String name; private final String description; private final Address address; public Account(String id, String name, String description, Address address) { this.id = id; this.name = name; this.description = description; this.address = address; } public String getId() { return id; } public String getName() { return name; } public String getDescription() { return description; } public Address getAddress() { return address; } } public class Address { private final String city; private final String zipCode; public Address(String city, String zipCode) { this.city = city; this.zipCode = zipCode; } public String getCity() { return city; } public String getZipCode() { return zipCode; } } X failed expecting [value] to equal {"id": "ac1", "name": "My Second Account", "address": {"zipCode": "7777777"}}: [value].name: actual: "My Account" <java.lang.String> expected: "My Second Account" <java.lang.String> ^ [value].address.zipCode: actual: "88888888" <java.lang.String> expected: "7777777" <java.lang.String> ^ (4ms) { "address": {"city": "TestingCity", "zipCode": **"88888888"**}, "description": "test account", "id": "ac1", "name": **"My Account"** }

Java Beans Contain Map

Use contain(map) to check if a collection of beans contains a bean with specified properties: Groovy def accounts = fetchAccounts() accounts.shouldNot contain([id: "ac2", name: "Work"]) // only check specified properties Java List<Account> accounts = fetchAccounts(); actual(accounts).shouldNot(contain(map("id", "ac2", "name", "Work"))); // only check specified properties public class Account { private final String id; private final String name; private final String description; private final Address address; public Account(String id, String name, String description, Address address) { this.id = id; this.name = name; this.description = description; this.address = address; } public String getId() { return id; } public String getName() { return name; } public String getDescription() { return description; } public Address getAddress() { return address; } } X failed expecting [value] to not contain {"id": "ac2", "name": "Work"}: [value][1].id: actual: "ac2" <java.lang.String> expected: not "ac2" <java.lang.String> [value][1].name: actual: "Work" <java.lang.String> expected: not "Work" <java.lang.String> (4ms) [ { "address": org.testingisdocumenting.webtau.Address@49ef32e0, "description": "test account", "id": "ac1", "name": "Home" }, { "address": org.testingisdocumenting.webtau.Address@271f18d3, "description": "test account", "id": **"ac2"**, "name": **"Work"** }, { "address": org.testingisdocumenting.webtau.Address@6bd51ed8, "description": "test account", "id": "ac3", "name": "My Account" } ]

Java Beans Equal Table Data

Use equal with TableData to compare with a collection of java beans. All records must be present. Groovy List<Account> accounts = fetchAccounts() TableData expected = ["*id" | "name" | "address"] { // id is a key column _________________________________________ "ac2" | "Works" | [zipCode: "zip2"] // when key is present, comparison is order agnostic "ac1" | "Home" | [zipCode: "zip1"] "ac3" | "My Account" | [zipCode: "zip8"] } accounts.should == expected Java List<Account> accounts = fetchAccounts(); TableData expected = table("*id", "name", "address", // id is a key column ________________________________________, "ac2", "Works", map("zipCode", "zip2"), // when key is present, comparison is order agnostic "ac1", "Home", map("zipCode", "zip1"), "ac3", "My Account", map("zipCode", "zip8")); actual(accounts).should(equal(expected)); Note: Only specified properties will be matched X failed expecting [value] to equal *id │ name │ address "ac2" │ "Works" │ {"zipCode": "zip2"} "ac1" │ "Home" │ {"zipCode": "zip1"} "ac3" │ "My Account" │ {"zipCode": "zip8"}: [value][2].address.zipCode: actual: "zip3" <java.lang.String> expected: "zip8" <java.lang.String> ^ [value][1].name: actual: "Work" <java.lang.String> expected: "Works" <java.lang.String> ^ (2ms) address │ description │ id │ name {"city": "TC1", "zipCode": "zip1"} │ "test account" │ "ac1" │ "Home" {"city": "TC2", "zipCode": "zip2"} │ "test account" │ "ac2" │ **"Work"** {"city": "TC3", "zipCode": **"zip3"**} │ "test account" │ "ac3" │ "My Account"

Java Beans Contain Table Data

Use contain with TableData to partially compare list of java beans. Groovy List<Account> accounts = fetchAccounts() TableData expected = ["*id" | "name" | "address"] { ____________________________________ "ac2" | "Works" | [zipCode: "zip2"] "ac1" | "Home" | [zipCode: "zip1"] } accounts.should contain(expected) X failed expecting accounts to contain id │ name │ address "ac2" │ "Works" │ {"zipCode": "zip2"} "ac1" │ "Home" │ {"zipCode": "zip1"}: no matches found for: [{"id": "ac2", "name": "Works", "address": {"zipCode": "zip2"}}] accounts[1].name: actual: "Work" <java.lang.String> expected: "Works" <java.lang.String> ^ (6ms) [ { "address": {"city": "TC1", "zipCode": "zip1"}, "description": "test account", "id": "ac1", "name": "Home" }, { "address": {"city": "TC2", "zipCode": "zip2"}, "description": "test account", "id": "ac2", "name": **"Work"** }, { "address": {"city": "TC3", "zipCode": "zip3"}, "description": "test account", "id": "ac3", "name": "My Account" } ] Java List<Account> accounts = fetchAccounts(); TableData expected = table("id", "name", "address", _______________________________________, "ac2", "Works", map("zipCode", "zip2"), "ac1", "Home", map("zipCode", "zip1")); actual(accounts, "accounts").should(contain(expected)); X failed expecting accounts to contain id │ name │ address "ac2" │ "Works" │ {"zipCode": "zip2"} "ac1" │ "Home" │ {"zipCode": "zip1"}: no matches found for: [{"id": "ac2", "name": "Works", "address": {"zipCode": "zip2"}}] accounts[1].name: actual: "Work" <java.lang.String> expected: "Works" <java.lang.String> ^ (6ms) [ { "address": {"city": "TC1", "zipCode": "zip1"}, "description": "test account", "id": "ac1", "name": "Home" }, { "address": {"city": "TC2", "zipCode": "zip2"}, "description": "test account", "id": "ac2", "name": **"Work"** }, { "address": {"city": "TC3", "zipCode": "zip3"}, "description": "test account", "id": "ac3", "name": "My Account" } ]

Java Records

WebTau compares Java Records with maps and tables in the same way as Java Beans. public record WishLitItem(String id, String description, boolean favorite, List<WishLitItem> related) { } Groovy def wishListItem = new WishLitItem("id1", "tea set", true, [new WishLitItem("id-nested", "tea", true, [])]) wishListItem.should == [ id: "id1", description: "tea sets", favorite: true, related: [ [id: "id-nested", description: "juice"]]] // only specified properties will be compared Java var wishListItem = new WishLitItem("id1", "tea set", true, list(new WishLitItem("id-nested", "tea", true, list()))); actual(wishListItem).should(equal(map( "id", "id1", "description", "tea sets", "favorite", true, "related", list(map( "id", "id-nested", "description", "juice"))))); // only specified properties will be compared X failed expecting [value] to equal { "id": "id1", "description": "tea sets", "favorite": true, "related": [{"id": "id-nested", "description": "juice"}] }: [value].description: actual: "tea set" <java.lang.String> expected: "tea sets" <java.lang.String> ^ [value].related[0].description: actual: "tea" <java.lang.String> expected: "juice" <java.lang.String> ^ (1ms) { "id": "id1", "description": **"tea set"**, "favorite": true, "related": [{"id": "id-nested", "description": **"tea"**, "favorite": true, "related": []}] }