[BAEL-16668] Split or move testing-modules/mockito module (#7835)
* Split or move testing-modules/mockito module * [BAEL-16668] fix after merge with master
This commit is contained in:
committed by
Josh Cummings
parent
4e31b41919
commit
2255577ffd
@@ -0,0 +1,12 @@
|
||||
## Hamcrest
|
||||
|
||||
This module contains articles about Hamcrest
|
||||
|
||||
### Relevant articles
|
||||
- [Hamcrest Text Matchers](https://www.baeldung.com/hamcrest-text-matchers)
|
||||
- [Hamcrest File Matchers](https://www.baeldung.com/hamcrest-file-matchers)
|
||||
- [Hamcrest Object Matchers](https://www.baeldung.com/hamcrest-object-matchers)
|
||||
- [Hamcrest Bean Matchers](https://www.baeldung.com/hamcrest-bean-matchers)
|
||||
- [Hamcrest Number Matchers](https://www.baeldung.com/hamcrest-number-matchers)
|
||||
- [Hamcrest Common Core Matchers](https://www.baeldung.com/hamcrest-core-matchers)
|
||||
- [Hamcrest Custom Matchers](https://www.baeldung.com/hamcrest-custom-matchers)
|
||||
@@ -0,0 +1,29 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>hamcrest</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>java-hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<hamcrest.version>2.0.0.0</hamcrest.version>
|
||||
</properties>
|
||||
</project>
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.hamcrest.objectmatchers;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.hamcrest.objectmatchers;
|
||||
|
||||
public class Location {
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.hamcrest;
|
||||
|
||||
import com.baeldung.hamcrest.objectmatchers.City;
|
||||
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.*;
|
||||
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"));
|
||||
}
|
||||
|
||||
}
|
||||
+256
@@ -0,0 +1,256 @@
|
||||
package com.baeldung.hamcrest;
|
||||
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.core.StringEndsWith.endsWith;
|
||||
import static org.hamcrest.core.StringStartsWith.startsWith;
|
||||
|
||||
public class HamcrestCoreMatchersUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingIsForMatch() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "hamcrest core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, is("hamcrest core"));
|
||||
assertThat(testString, is(equalTo("hamcrest core")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDifferentStaticTypeTestInput_WhenUsingEqualToObject_ThenCorrect() {
|
||||
|
||||
// GIVEN
|
||||
Object original = 100;
|
||||
|
||||
// ASSERT
|
||||
assertThat(original, equalToObject(100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingInstanceOfForClassTypeCheck() {
|
||||
|
||||
assertThat("hamcrest", is(instanceOf(String.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingIsA_ThenAssertType() {
|
||||
|
||||
assertThat("hamcrest core", isA(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingEqualToMatcherForEquality() {
|
||||
|
||||
// GIVEN
|
||||
String actualString = "Hamcrest Core";
|
||||
List<String> actualList = Lists.newArrayList("hamcrest", "core");
|
||||
|
||||
// ASSERT
|
||||
assertThat(actualString, is(equalTo("Hamcrest Core")));
|
||||
assertThat(actualList, is(equalTo(Lists.newArrayList("hamcrest", "core"))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingNotForMatch() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "hamcrest";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, not("hamcrest core"));
|
||||
assertThat(testString, is(not(equalTo("hamcrest core"))));
|
||||
assertThat(testString, is(not(instanceOf(Integer.class))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingNullValueForNullCheck() {
|
||||
|
||||
// GIVEN
|
||||
Integer nullObject = null;
|
||||
|
||||
// ASSERT
|
||||
assertThat(nullObject, is(nullValue()));
|
||||
assertThat(nullObject, is(nullValue(Integer.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingNotNullValueForNotNullCheck() {
|
||||
|
||||
// GIVEN
|
||||
Integer testNumber = 123;
|
||||
|
||||
// ASSERT
|
||||
assertThat(testNumber, is(notNullValue()));
|
||||
assertThat(testNumber, is(notNullValue(Integer.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_WhenStartsWith_ThenCorrect() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "hamcrest core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, startsWith("hamcrest"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giveString_WhenStartsWithIgnoringCase_ThenCorrect() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "hamcrest core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, startsWithIgnoringCase("HAMCREST"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_WhenEndsWith_ThenCorrect() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "hamcrest core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, endsWith("core"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_WhenEndsWithIgnoringCase_ThenCorrect() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "hamcrest core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, endsWithIgnoringCase("CORE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_WhenContainsString_ThenCorrect() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "hamcrest core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, containsString("co"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenString_WhenContainsStringIgnoringCase_ThenCorrect() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "hamcrest core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, containsStringIgnoringCase("CO"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingHasItemInCollection() {
|
||||
|
||||
// GIVEN
|
||||
List<String> list = Lists.newArrayList("java", "spring", "baeldung");
|
||||
|
||||
// ASSERT
|
||||
assertThat(list, hasItem("java"));
|
||||
assertThat(list, hasItem(isA(String.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingHasItemsInCollection() {
|
||||
|
||||
// GIVEN
|
||||
List<String> list = Lists.newArrayList("java", "spring", "baeldung");
|
||||
|
||||
// ASSERT
|
||||
assertThat(list, hasItems("java", "baeldung"));
|
||||
assertThat(list, hasItems(isA(String.class), endsWith("ing")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingAnyForClassType() {
|
||||
|
||||
assertThat("hamcrest", is(any(String.class)));
|
||||
assertThat("hamcrest", is(any(Object.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingAllOfForAllMatchers() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "Hamcrest Core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, allOf(startsWith("Ham"), endsWith("ore"), containsString("Core")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingAnyOfForAnyMatcher() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "Hamcrest Core";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, anyOf(startsWith("Ham"), containsString("baeldung")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingBothForMatcher() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "Hamcrest Core Matchers";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, both(startsWith("Ham")).and(containsString("Core")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingEitherForMatcher() {
|
||||
|
||||
// GIVEN
|
||||
String testString = "Hamcrest Core Matchers";
|
||||
|
||||
// ASSERT
|
||||
assertThat(testString, either(startsWith("Bael")).or(containsString("Core")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTestInput_WhenUsingEveryItemForMatchInCollection() {
|
||||
|
||||
// GIVEN
|
||||
List<String> testItems = Lists.newArrayList("Common", "Core", "Combinable");
|
||||
|
||||
// ASSERT
|
||||
assertThat(testItems, everyItem(startsWith("Co")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoTestInputs_WhenUsingSameInstanceForMatch() {
|
||||
|
||||
// GIVEN
|
||||
String string1 = "hamcrest";
|
||||
String string2 = string1;
|
||||
|
||||
// ASSERT
|
||||
assertThat(string1, is(sameInstance(string2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoTestInputs_WhenUsingTheInstanceForMatch() {
|
||||
// GIVEN
|
||||
String string1 = "hamcrest";
|
||||
String string2 = string1;
|
||||
|
||||
// ASSERT
|
||||
assertThat(string1, is(theInstance(string2)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.hamcrest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import static org.hamcrest.io.FileMatchers.*;
|
||||
import static org.hamcrest.number.OrderingComparison.greaterThan;
|
||||
import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class HamcrestFileUnitTest {
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingFileName_thenCorrect() {
|
||||
File file = new File("src/test/resources/test1.in");
|
||||
|
||||
assertThat(file, aFileNamed(equalToIgnoringCase("test1.in")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingFileOrDirExist_thenCorrect() {
|
||||
File file = new File("src/test/resources/test1.in");
|
||||
File dir = new File("src/test/resources");
|
||||
|
||||
assertThat(file, anExistingFile());
|
||||
assertThat(dir, anExistingDirectory());
|
||||
assertThat(file, anExistingFileOrDirectory());
|
||||
assertThat(dir, anExistingFileOrDirectory());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingFileIsReadableAndWritable_thenCorrect() {
|
||||
File file = new File("src/test/resources/test1.in");
|
||||
|
||||
assertThat(file, aReadableFile());
|
||||
assertThat(file, aWritableFile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyingFileSize_thenCorrect() {
|
||||
File file = new File("src/test/resources/test1.in");
|
||||
|
||||
assertThat(file, aFileWithSize(11));
|
||||
assertThat(file, aFileWithSize(greaterThan(1L)));;
|
||||
}
|
||||
|
||||
/*@Test
|
||||
public final void whenVerifyingFilePath_thenCorrect() {
|
||||
File file = new File("src/test/resources/test1.in");
|
||||
|
||||
assertThat(file, aFileWithCanonicalPath(containsString("src/test/resources")));
|
||||
assertThat(file, aFileWithAbsolutePath(containsString("src/test/resources")));
|
||||
}*/
|
||||
}
|
||||
+170
@@ -0,0 +1,170 @@
|
||||
package com.baeldung.hamcrest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
public class HamcrestNumberUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenADouble_whenCloseTo_thenCorrect() {
|
||||
double actual = 1.3;
|
||||
double operand = 1;
|
||||
double error = 0.5;
|
||||
assertThat(actual, is(closeTo(operand, error)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenADouble_whenNotCloseTo_thenCorrect() {
|
||||
double actual = 1.6;
|
||||
double operand = 1;
|
||||
double error = 0.5;
|
||||
assertThat(actual, is(not(closeTo(operand, error))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenABigDecimal_whenCloseTo_thenCorrect() {
|
||||
BigDecimal actual = new BigDecimal("1.0003");
|
||||
BigDecimal operand = new BigDecimal("1");
|
||||
BigDecimal error = new BigDecimal("0.0005");
|
||||
assertThat(actual, is(closeTo(operand, error)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenABigDecimal_whenNotCloseTo_thenCorrect() {
|
||||
BigDecimal actual = new BigDecimal("1.0006");
|
||||
BigDecimal operand = new BigDecimal("1");
|
||||
BigDecimal error = new BigDecimal("0.0005");
|
||||
assertThat(actual, is(not(closeTo(operand, error))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given5_whenComparesEqualTo5_thenCorrect() {
|
||||
Integer five = 5;
|
||||
assertThat(five, comparesEqualTo(five));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given5_whenNotComparesEqualTo7_thenCorrect() {
|
||||
Integer seven = 7;
|
||||
Integer five = 5;
|
||||
assertThat(five, not(comparesEqualTo(seven)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given7_whenGreaterThan5_thenCorrect() {
|
||||
Integer seven = 7;
|
||||
Integer five = 5;
|
||||
assertThat(seven, is(greaterThan(five)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given7_whenGreaterThanOrEqualTo5_thenCorrect() {
|
||||
Integer seven = 7;
|
||||
Integer five = 5;
|
||||
assertThat(seven, is(greaterThanOrEqualTo(five)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given5_whenGreaterThanOrEqualTo5_thenCorrect() {
|
||||
Integer five = 5;
|
||||
assertThat(five, is(greaterThanOrEqualTo(five)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given3_whenLessThan5_thenCorrect() {
|
||||
Integer three = 3;
|
||||
Integer five = 5;
|
||||
assertThat(three, is(lessThan(five)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given3_whenLessThanOrEqualTo5_thenCorrect() {
|
||||
Integer three = 3;
|
||||
Integer five = 5;
|
||||
assertThat(three, is(lessThanOrEqualTo(five)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given5_whenLessThanOrEqualTo5_thenCorrect() {
|
||||
Integer five = 5;
|
||||
assertThat(five, is(lessThanOrEqualTo(five)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBenjamin_whenGreaterThanAmanda_thenCorrect() {
|
||||
String amanda = "Amanda";
|
||||
String benjamin = "Benjamin";
|
||||
assertThat(benjamin, is(greaterThan(amanda)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAmanda_whenLessThanBenajmin_thenCorrect() {
|
||||
String amanda = "Amanda";
|
||||
String benjamin = "Benjamin";
|
||||
assertThat(amanda, is(lessThan(benjamin)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToday_whenGreaterThanYesterday_thenCorrect() {
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate yesterday = today.minusDays(1);
|
||||
assertThat(today, is(greaterThan(yesterday)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenToday_whenLessThanTomorrow_thenCorrect() {
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate tomorrow = today.plusDays(1);
|
||||
assertThat(today, is(lessThan(tomorrow)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAmanda_whenOlderThanBenjamin_thenCorrect() {
|
||||
Person amanda = new Person("Amanda", 20);
|
||||
Person benjamin = new Person("Benjamin", 18);
|
||||
assertThat(amanda, is(greaterThan(benjamin)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBenjamin_whenYoungerThanAmanda_thenCorrect() {
|
||||
Person amanda = new Person("Amanda", 20);
|
||||
Person benjamin = new Person("Benjamin", 18);
|
||||
assertThat(benjamin, is(lessThan(amanda)));
|
||||
}
|
||||
|
||||
class Person implements Comparable<Person> {
|
||||
String name;
|
||||
int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Person o) {
|
||||
if (this.age == o.getAge()) return 0;
|
||||
if (this.age > o.age) return 1;
|
||||
else return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNaN_whenIsNotANumber_thenCorrect() {
|
||||
double zero = 0d;
|
||||
assertThat(zero / zero, is(notANumber()));
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.hamcrest;
|
||||
|
||||
import com.baeldung.hamcrest.objectmatchers.City;
|
||||
import com.baeldung.hamcrest.objectmatchers.Location;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package com.baeldung.hamcrest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.core.StringContains.containsString;
|
||||
import static org.hamcrest.core.StringContains.containsStringIgnoringCase;
|
||||
import static org.hamcrest.core.StringEndsWith.endsWith;
|
||||
import static org.hamcrest.core.StringEndsWith.endsWithIgnoringCase;
|
||||
import static org.hamcrest.core.StringStartsWith.startsWith;
|
||||
import static org.hamcrest.core.StringStartsWith.startsWithIgnoringCase;
|
||||
import static org.hamcrest.text.IsBlankString.blankOrNullString;
|
||||
import static org.hamcrest.text.IsBlankString.blankString;
|
||||
import static org.hamcrest.text.IsEmptyString.emptyOrNullString;
|
||||
import static org.hamcrest.text.IsEmptyString.emptyString;
|
||||
import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
|
||||
import static org.hamcrest.text.IsEqualIgnoringWhiteSpace.equalToIgnoringWhiteSpace;
|
||||
import static org.hamcrest.text.MatchesPattern.matchesPattern;
|
||||
import static org.hamcrest.text.StringContainsInOrder.stringContainsInOrder;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class HamcrestTextUnitTest {
|
||||
|
||||
@Test
|
||||
public final void whenTwoStringsAreEqual_thenCorrect() {
|
||||
String first = "hello";
|
||||
String second = "Hello";
|
||||
|
||||
assertThat(first, equalToIgnoringCase(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenTwoStringsAreEqualWithWhiteSpace_thenCorrect() {
|
||||
String first = "hello";
|
||||
String second = " Hello ";
|
||||
|
||||
assertThat(first, equalToIgnoringWhiteSpace(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenStringIsBlank_thenCorrect() {
|
||||
String first = " ";
|
||||
String second = null;
|
||||
|
||||
assertThat(first, blankString());
|
||||
assertThat(first, blankOrNullString());
|
||||
assertThat(second, blankOrNullString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenStringIsEmpty_thenCorrect() {
|
||||
String first = "";
|
||||
String second = null;
|
||||
|
||||
assertThat(first, emptyString());
|
||||
assertThat(first, emptyOrNullString());
|
||||
assertThat(second, emptyOrNullString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenStringMatchPattern_thenCorrect() {
|
||||
String first = "hello";
|
||||
|
||||
assertThat(first, matchesPattern("[a-z]+"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyStringContains_thenCorrect() {
|
||||
String first = "hello";
|
||||
|
||||
assertThat(first, containsString("lo"));
|
||||
assertThat(first, containsStringIgnoringCase("EL"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyStringContainsInOrder_thenCorrect() {
|
||||
String first = "hello";
|
||||
|
||||
assertThat(first, stringContainsInOrder("e","l","o"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyStringStartsWith_thenCorrect() {
|
||||
String first = "hello";
|
||||
|
||||
assertThat(first, startsWith("he"));
|
||||
assertThat(first, startsWithIgnoringCase("HEL"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenVerifyStringEndsWith_thenCorrect() {
|
||||
String first = "hello";
|
||||
|
||||
assertThat(first, endsWith("lo"));
|
||||
assertThat(first, endsWithIgnoringCase("LO"));
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.hamcrest.custommatchers;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static com.baeldung.hamcrest.custommatchers.IsDivisibleBy.divisibleBy;
|
||||
import static com.baeldung.hamcrest.custommatchers.IsOnlyDigits.onlyDigits;
|
||||
import static com.baeldung.hamcrest.custommatchers.IsUppercase.uppercase;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
|
||||
public class HamcrestCustomUnitTest {
|
||||
|
||||
@Test
|
||||
public final void givenAString_whenIsOnlyDigits_thenCorrect() {
|
||||
String digits = "123";
|
||||
|
||||
assertThat(digits, is(onlyDigits()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenAString_whenIsNotOnlyDigits_thenCorrect() {
|
||||
String aphanumeric = "123ABC";
|
||||
|
||||
assertThat(aphanumeric, is(not(onlyDigits())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenAString_whenIsUppercase_thenCorrect() {
|
||||
String uppercaseWord = "HELLO";
|
||||
|
||||
assertThat(uppercaseWord, is(uppercase()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenAnEvenInteger_whenDivisibleByTwo_thenCorrect() {
|
||||
Integer ten = 10;
|
||||
Integer two = 2;
|
||||
|
||||
assertThat(ten, is(divisibleBy(two)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void givenAnOddInteger_whenNotDivisibleByTwo_thenCorrect() {
|
||||
Integer eleven = 11;
|
||||
Integer two = 2;
|
||||
|
||||
assertThat(eleven, is(not(divisibleBy(two))));
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.hamcrest.custommatchers;
|
||||
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.TypeSafeMatcher;
|
||||
|
||||
public class IsDivisibleBy extends TypeSafeMatcher<Integer> {
|
||||
|
||||
private Integer divider;
|
||||
|
||||
private IsDivisibleBy(Integer divider) {
|
||||
this.divider = divider;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean matchesSafely(Integer dividend) {
|
||||
if (divider == 0) return false;
|
||||
return ((dividend % divider) == 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("divisible by " + divider);
|
||||
}
|
||||
|
||||
public static Matcher<Integer> divisibleBy(Integer divider) {
|
||||
return new IsDivisibleBy(divider);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.hamcrest.custommatchers;
|
||||
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.TypeSafeMatcher;
|
||||
|
||||
public class IsOnlyDigits extends TypeSafeMatcher<String> {
|
||||
|
||||
@Override
|
||||
protected boolean matchesSafely(String s) {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
return true;
|
||||
} catch (NumberFormatException nfe) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("only digits");
|
||||
}
|
||||
|
||||
public static Matcher<String> onlyDigits() {
|
||||
return new IsOnlyDigits();
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.hamcrest.custommatchers;
|
||||
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.TypeSafeMatcher;
|
||||
|
||||
public class IsUppercase extends TypeSafeMatcher<String> {
|
||||
|
||||
@Override
|
||||
protected boolean matchesSafely(String s) {
|
||||
return s.equals(s.toUpperCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("all uppercase");
|
||||
}
|
||||
|
||||
public static Matcher<String> uppercase() {
|
||||
return new IsUppercase();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Hello world
|
||||
Reference in New Issue
Block a user