BAEL-1578: Hamcrest Object and Beans matchers. (#3820)

This commit is contained in:
Magdalena Krause 2018-03-15 19:42:11 -03:00 committed by maibin
parent a526bed5c2
commit b508689815
4 changed files with 193 additions and 0 deletions

View File

@ -0,0 +1,41 @@
package org.baeldung.hamcrest;
public class City extends Location {
String name;
String state;
public City(String name, String state) {
this.name = name;
this.state = state;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setState(String state) {
this.state = state;
}
public String getState() {
return state;
}
@Override
public String toString() {
if (this.name == null && this.state == null) return null;
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append("Name: ");
sb.append(this.name);
sb.append(", ");
sb.append("State: ");
sb.append(this.state);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,4 @@
package org.baeldung.hamcrest;
public class Location {
}

View File

@ -0,0 +1,91 @@
package org.baeldung.hamcrest;
import org.junit.Test;
import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.List;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.samePropertyValuesAs;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.beans.PropertyUtil.getPropertyDescriptor;
import static org.hamcrest.beans.PropertyUtil.propertyDescriptorsFor;
public class HamcrestBeansUnitTest {
@Test
public void givenACity_whenHasProperty_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city, hasProperty("name"));
}
@Test
public void givenACity_whenNotHasProperty_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city, not(hasProperty("country")));
}
@Test
public void givenACity_whenHasPropertyWithValueEqualTo_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city, hasProperty("name", equalTo("San Francisco")));
}
@Test
public void givenACity_whenHasPropertyWithValueEqualToIgnoringCase_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city, hasProperty("state", equalToIgnoringCase("ca")));
}
@Test
public void givenACity_whenSamePropertyValuesAs_thenCorrect() {
City city = new City("San Francisco", "CA");
City city2 = new City("San Francisco", "CA");
assertThat(city, samePropertyValuesAs(city2));
}
@Test
public void givenACity_whenNotSamePropertyValuesAs_thenCorrect() {
City city = new City("San Francisco", "CA");
City city2 = new City("Los Angeles", "CA");
assertThat(city, not(samePropertyValuesAs(city2)));
}
@Test
public void givenACity_whenGetPropertyDescriptor_thenCorrect() {
City city = new City("San Francisco", "CA");
PropertyDescriptor descriptor = getPropertyDescriptor("state", city);
assertThat(descriptor
.getReadMethod()
.getName(), is(equalTo("getState")));
}
@Test
public void givenACity_whenGetPropertyDescriptorsFor_thenCorrect() {
City city = new City("San Francisco", "CA");
PropertyDescriptor[] descriptors = propertyDescriptorsFor(city, Object.class);
List<String> getters = Arrays
.stream(descriptors)
.map(x -> x
.getReadMethod()
.getName())
.collect(toList());
assertThat(getters, containsInAnyOrder("getName", "getState"));
}
}

View File

@ -0,0 +1,57 @@
package org.baeldung.hamcrest;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.typeCompatibleWith;
import static org.hamcrest.Matchers.not;
public class HamcrestObjectUnitTest {
@Test
public void givenACity_whenHasToString_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city, hasToString("[Name: San Francisco, State: CA]"));
}
@Test
public void givenACity_whenHasToStringEqualToIgnoringCase_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city, hasToString(equalToIgnoringCase("[NAME: SAN FRANCISCO, STATE: CA]")));
}
@Test
public void givenACity_whenHasToStringEmptyOrNullString_thenCorrect() {
City city = new City(null, null);
assertThat(city, hasToString(emptyOrNullString()));
}
@Test
public void givenACity_whenTypeCompatibleWithLocation_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city.getClass(), is(typeCompatibleWith(Location.class)));
}
@Test
public void givenACity_whenTypeNotCompatibleWithString_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city.getClass(), is(not(typeCompatibleWith(String.class))));
}
@Test
public void givenACity_whenTypeCompatibleWithObject_thenCorrect() {
City city = new City("San Francisco", "CA");
assertThat(city.getClass(), is(typeCompatibleWith(Object.class)));
}
}