Last active
October 23, 2021 17:04
-
-
Save rahulrathore44/a1a71b79a94e070861a7914d9a1fe575 to your computer and use it in GitHub Desktop.
Matcher created using Hamcrest framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.automation.model; | |
public class Address { | |
private int zipCode; | |
public int getZipCode() { | |
return zipCode; | |
} | |
public String getStreet() { | |
return street; | |
} | |
private String street; | |
public Address(int zipCode, String street) { | |
this.zipCode = zipCode; | |
this.street = street; | |
} | |
@Override | |
public String toString() { | |
return String.format("zipcode: %d street: %s", zipCode, street); | |
} | |
} | |
/*-------------------------------------------- AddressMatcher---------------------------------------*/ | |
package com.automation.model.matchers; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
import com.automation.model.Address; | |
public class AddressMatcher extends TypeSafeMatcher<Address> { | |
private Matcher<Integer> zipCode; // Matchers.is(100) | |
private Matcher<String> street; // Matchers.is("John") | |
public AddressMatcher(Matcher<Integer> zipCode, Matcher<String> street) { | |
this.zipCode = zipCode; | |
this.street = street; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendDescriptionOf(zipCode).appendDescriptionOf(street); | |
} | |
@Override | |
protected boolean matchesSafely(Address item) { | |
return zipCode.matches(item.getZipCode()) && street.matches(item.getStreet()); | |
} | |
public static Matcher<Address> matchAddress(Matcher<Integer> zipCode, Matcher<String> street) { | |
return new AddressMatcher(zipCode, street); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hamcrest.MatcherAssert; | |
import org.hamcrest.Matchers; | |
import org.junit.jupiter.api.Test; | |
public class AppTest { | |
@Test | |
public void testStringObject() { | |
String actual = "Hello"; | |
MatcherAssert.assertThat(actual, Matchers.is("Hello")); | |
// The content of actual string object is "Hello" | |
// Matchers -> Set of conditions which either return true/false | |
MatcherAssert.assertThat(actual, Matchers.not("Cat")); | |
// the content of actual string object is not "Cat" | |
MatcherAssert.assertThat(actual, Matchers.not(Matchers.isEmptyString())); | |
// the content of actual string object is not a empty string | |
MatcherAssert.assertThat(actual, Matchers.allOf(Matchers.is("Hello"), Matchers.not("Cat"),Matchers.not(Matchers.isEmptyString()), | |
Matchers.startsWith("He"))); | |
} | |
@Test | |
public void testInteger() { | |
Integer number = Integer.valueOf(10); | |
MatcherAssert.assertThat(number, Matchers.greaterThan(5)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.automation.model; | |
public enum BillType { | |
DAY_USE_ONLY,NIGHT_USE_ONLY | |
} | |
package com.automation.model; | |
public class BillAmount { | |
private Integer amount; | |
public Integer getAmount() { | |
return amount; | |
} | |
public BillType getType() { | |
return type; | |
} | |
private BillType type; | |
public BillAmount() { | |
amount = 0; | |
type = BillType.DAY_USE_ONLY; | |
} | |
public BillAmount(Integer amount, BillType type) { | |
this.amount = amount; | |
this.type = type; | |
} | |
@Override | |
public String toString() { | |
return String.format("amount: %d billtype: %s", amount, type); | |
} | |
} | |
/*-------------------------------------------- BillAmountMatcher---------------------------------------*/ | |
package com.automation.model.matchers; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
import com.automation.model.BillAmount; | |
import com.automation.model.BillType; | |
public class BillAmountMatcher extends TypeSafeMatcher<BillAmount> { | |
private Matcher<Integer> amount; | |
private Matcher<BillType> type; | |
public BillAmountMatcher(Matcher<Integer> amount, Matcher<BillType> type) { | |
this.amount = amount; | |
this.type = type; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendDescriptionOf(amount).appendDescriptionOf(type); | |
} | |
@Override | |
protected boolean matchesSafely(BillAmount item) { | |
return amount.matches(item.getAmount()) && type.matches(item.getType()); | |
} | |
public static Matcher<BillAmount> matchBillAmount(Matcher<Integer> amount, Matcher<BillType> type) { | |
return new BillAmountMatcher(amount, type); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.automation.model; | |
public class Customer { | |
private String name; | |
public String getName() { | |
return name; | |
} | |
public Address getAddress() { | |
return address; | |
} | |
private Address address; | |
public Customer(String name, Address address) { | |
this.name = name; | |
this.address = address; | |
} | |
@Override | |
public String toString() { | |
return String.format("name: %s address: %s", name, address); | |
} | |
} | |
/*-------------------------------------------- CustomerMatcher---------------------------------------*/ | |
package com.automation.model.matchers; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
import com.automation.model.Address; | |
import com.automation.model.Customer; | |
public class CustomerMatcher extends TypeSafeMatcher<Customer> { | |
private Matcher<String> name; | |
private Matcher<Address> address; | |
public CustomerMatcher(Matcher<String> name, Matcher<Address> address) { | |
this.address = address; | |
this.name = name; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendDescriptionOf(name).appendDescriptionOf(address); | |
} | |
@Override | |
protected boolean matchesSafely(Customer item) { | |
return name.matches(item.getName()) && address.matches(item.getAddress()); | |
} | |
public static Matcher<Customer> matchCustomer(Matcher<String> name, Matcher<Address> address) { | |
return new CustomerMatcher(name, address); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hamcrest.Description; | |
import org.hamcrest.TypeSafeMatcher; | |
public class CustomIntegerMatcher extends TypeSafeMatcher<Integer> { | |
@Override | |
public void describeTo(Description description) { | |
description.appendText(" Value must be greater than 10 "); | |
} | |
@Override | |
protected boolean matchesSafely(Integer item) { | |
if( item.intValue() > 10) | |
return true; | |
return false; | |
} | |
// Step 1. Extend the class called TypeSafeMatcher<T> | |
// Step 2. Implement the matcher rules (conditions) | |
// Step 3. Create the static method. The method will return the object of custom matcher | |
public static CustomIntegerMatcher numberGrtThan10() { | |
return new CustomIntegerMatcher(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hamcrest.Description; | |
import org.hamcrest.TypeSafeMatcher; | |
public class CustomIntegerMatcherWithParams extends TypeSafeMatcher<Integer> { | |
private Integer expectedValue; | |
public CustomIntegerMatcherWithParams(Integer expectedValue) { | |
this.expectedValue = expectedValue; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText(" Value must be greater than " + expectedValue.intValue()); | |
} | |
@Override | |
protected boolean matchesSafely(Integer item) { | |
if( item.intValue() > expectedValue.intValue()) | |
return true; | |
return false; | |
} | |
// Step 1. Extend the class called TypeSafeMatcher<T> | |
// Step 2. Implement the matcher rules (conditions) | |
// Step 3. Create the static method. The method will return the object of custom matcher | |
public static CustomIntegerMatcherWithParams numberGrtThan(Integer expectedValue) { | |
return new CustomIntegerMatcherWithParams(expectedValue); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
public class CustomIntegerMatcherWithType extends TypeSafeMatcher<Integer> { | |
private Matcher<Integer> expectedValue; | |
public CustomIntegerMatcherWithType(Matcher<Integer> expectedValue) { | |
this.expectedValue = expectedValue; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendDescriptionOf(expectedValue); | |
} | |
@Override | |
protected boolean matchesSafely(Integer item) { | |
boolean outcome = expectedValue.matches(item); | |
return outcome; | |
} | |
// Step 1. Extend the class called TypeSafeMatcher<T> | |
// Step 2. Implement the matcher rules (conditions) | |
// Step 3. Create the static method. The method will return the object of custom matcher | |
public static CustomIntegerMatcherWithType numberMatcher(Matcher<Integer> expectedValue) { | |
return new CustomIntegerMatcherWithType(expectedValue); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.automation.model; | |
public class EnergyBill { | |
private Integer id; | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public BillAmount getAmount() { | |
return amount; | |
} | |
public void setAmount(BillAmount amount) { | |
this.amount = amount; | |
} | |
public Customer getCustomer() { | |
return customer; | |
} | |
public void setCustomer(Customer customer) { | |
this.customer = customer; | |
} | |
private BillAmount amount; | |
private Customer customer; | |
@Override | |
public String toString() { | |
return String.format("id: %d customer: %s billamount: %s", id, customer, amount); | |
} | |
} | |
/*-------------------------------------------- EnergyBillMatcher---------------------------------------*/ | |
package com.automation.model.matchers; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
import org.hamcrest.TypeSafeMatcher; | |
import com.automation.model.BillAmount; | |
import com.automation.model.Customer; | |
import com.automation.model.EnergyBill; | |
public class EnergyBillMatcher extends TypeSafeMatcher<EnergyBill> { | |
private Matcher<BillAmount> amount; | |
private Matcher<Customer> customer; | |
private Matcher<Integer> id; | |
public EnergyBillMatcher(Matcher<Integer> id, Matcher<Customer> customer, Matcher<BillAmount> amount) { | |
this.id = id; | |
this.customer = customer; | |
this.amount = amount; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendDescriptionOf(id).appendDescriptionOf(customer).appendDescriptionOf(amount); | |
} | |
@Override | |
protected boolean matchesSafely(EnergyBill item) { | |
return id.matches(item.getId()) && customer.matches(item.getCustomer()) && amount.matches(item.getAmount()); | |
} | |
public static Matcher<EnergyBill> matchEnergyBill(Matcher<Integer> id, Matcher<Customer> customer, | |
Matcher<BillAmount> amount) { | |
return new EnergyBillMatcher(id, customer, amount); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.hamcrest.MatcherAssert; | |
import org.junit.jupiter.api.Test; | |
import com.automation.custommatcher.CustomIntegerMatcher; | |
import com.automation.custommatcher.CustomIntegerMatcherWithParams; | |
public class TestCustomMatcher { | |
@Test | |
public void testInteger() { | |
Integer number = Integer.valueOf(100); | |
MatcherAssert.assertThat(number, CustomIntegerMatcher.numberGrtThan10()); | |
} | |
@Test | |
public void testIntegerWithParams() { | |
Integer number = Integer.valueOf(1010); | |
MatcherAssert.assertThat(number, CustomIntegerMatcherWithParams.numberGrtThan(100)); | |
} | |
@Test | |
public void testIntegerWithType() { | |
Integer number = Integer.valueOf(1010); | |
MatcherAssert.assertThat(number, CustomIntegerMatcherWithType.matchInteger(Matchers.is(1010))); | |
// the content of the number object is 1010 | |
MatcherAssert.assertThat(number, CustomIntegerMatcherWithType.matchInteger(Matchers.greaterThan(90))); | |
// the content of the number object is greater than 1010 | |
MatcherAssert.assertThat(number, CustomIntegerMatcherWithType.matchInteger(Matchers.allOf( | |
Matchers.greaterThan(90), Matchers.lessThan(10101)))); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.automation.model.test; | |
import static com.automation.model.matchers.AddressMatcher.matchAddress; | |
import static com.automation.model.matchers.BillAmountMatcher.matchBillAmount; | |
import static com.automation.model.matchers.CustomerMatcher.matchCustomer; | |
import static com.automation.model.matchers.EnergyBillMatcher.matchEnergyBill; | |
import static org.hamcrest.Matchers.is; | |
import static org.hamcrest.Matchers.lessThan; | |
import org.hamcrest.MatcherAssert; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import com.automation.model.Address; | |
import com.automation.model.BillAmount; | |
import com.automation.model.BillType; | |
import com.automation.model.Customer; | |
import com.automation.model.EnergyBill; | |
public class TestEnergyBill { | |
private EnergyBill eBill; | |
private Integer _id = 100; | |
@BeforeEach | |
public void setUp() { | |
eBill = new EnergyBill(); | |
eBill.setId(_id); | |
eBill.setCustomer(new Customer("John", new Address(12345, "22 Baker Street"))); | |
eBill.setAmount(new BillAmount(120, BillType.NIGHT_USE_ONLY)); | |
} | |
@Test | |
public void testEnergyBillObject() { | |
MatcherAssert.assertThat(eBill, | |
matchEnergyBill(is(100), matchCustomer(is("John"), matchAddress(is(12345), is("22 Baker Street"))), | |
matchBillAmount(lessThan(150), is(BillType.NIGHT_USE_ONLY)))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment