Commits

Micha Kops committed 992adf5 Draft

Initial import, created some custom typesafe matchers for a book.

  • Participants

Comments (0)

Files changed (6)

+.settings
+.project
+target
+.classpath
+# Hamcrest Custom Matcher Samples
+
+Some simple examples for implementing custom hamcrest matchers for your tests.
+
+---
+
+**2012 Micha Kops / hasCode.com**
+<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.hascode.tutorial</groupId>
+	<artifactId>hamcrest-matcher-sample</artifactId>
+	<version>0.0.1</version>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>junit</groupId>
+			<artifactId>junit</artifactId>
+			<version>4.10</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
+			<groupId>org.hamcrest</groupId>
+			<artifactId>hamcrest-all</artifactId>
+			<version>1.3</version>
+			<scope>test</scope>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+</project>

File src/main/java/com/hascode/entity/Book.java

+package com.hascode.entity;
+
+public class Book {
+	private final Long id;
+	private final String isbn;
+	private final String title;
+
+	public Book(final Long id, final String isbn, final String title) {
+		this.id = id;
+		this.isbn = isbn;
+		this.title = title;
+	}
+
+	public Long getId() {
+		return id;
+	}
+
+	public String getIsbn() {
+		return isbn;
+	}
+
+	public String getTitle() {
+		return title;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see java.lang.Object#toString()
+	 */
+	@Override
+	public String toString() {
+		return "Book [id=" + id + ", isbn=" + isbn + ", title=" + title + "]";
+	}
+}

File src/test/java/com/hascode/entity/BookTest.java

+package com.hascode.entity;
+
+import static com.hascode.matcher.BookMatcher.hasId;
+import static com.hascode.matcher.BookMatcher.hasIsbn;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class BookTest {
+	@Test
+	public void testBookIsbn() {
+		Book book = new Book(1l, "5555", "A book");
+		assertThat(book, hasIsbn("1234"));
+	}
+
+	@Test
+	public void testBookId() {
+		Book book = new Book(23l, "5555", "A book");
+		assertThat(book, hasId(3l));
+	}
+}

File src/test/java/com/hascode/matcher/BookMatcher.java

+package com.hascode.matcher;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+import com.hascode.entity.Book;
+
+public class BookMatcher {
+	public static Matcher<Book> hasIsbn(final String isbn) {
+		return new TypeSafeMatcher<Book>() {
+
+			@Override
+			public void describeTo(final Description description) {
+				description.appendText("expected result from getIsbn(): ")
+						.appendValue(isbn);
+			}
+
+			@Override
+			public boolean matchesSafely(final Book book) {
+				return isbn.equals(book.getIsbn());
+			}
+
+			@Override
+			public void describeMismatchSafely(final Book book,
+					final Description mismatchDescription) {
+				mismatchDescription.appendText(" but was ").appendValue(
+						book.getIsbn());
+			}
+
+		};
+	}
+
+	public static Matcher<Book> hasId(final Long id) {
+		return new TypeSafeMatcher<Book>() {
+
+			@Override
+			public void describeTo(final Description description) {
+				description.appendText("expected result from getId(): ")
+						.appendValue(id);
+			}
+
+			@Override
+			protected boolean matchesSafely(final Book book) {
+				return id == book.getId();
+			}
+		};
+	}
+}