Created
April 4, 2012 21:59
-
-
Save rschumm/2306009 to your computer and use it in GitHub Desktop.
JUnit Matcher / Hamcrest
This file contains 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
mit dem Matcher können komplexere Vergleiche mit "assertThat" getestet werden, hier ein einfaches Beispiel, | |
welches testet, dass zwei Daten am gleichen Tag sind. | |
import org.hamcrest.BaseMatcher; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
... | |
class TerminTestUtils ... | |
.... | |
public static Matcher<Date> gleicherTag(final Object dateSoll) { | |
final SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy"); | |
return new BaseMatcher<Date>() { | |
public void describeTo(Description desc) { | |
desc.appendText(df.format(dateSoll)); | |
} | |
public boolean matches(Object dateIst) { | |
if (dateIst == null || dateSoll == null) { | |
return false; | |
} | |
return df.format(dateIst).equals(df.format(dateSoll)); | |
} | |
}; | |
} | |
wird im JUnit-Test benutzt mit: | |
assertThat("Testfall " + testvektor.nr + ": Termin: ", businessFunctionK3 | |
.getAgendaNotizFaelligkeitZeitpunkt(), TerminTestUtils.gleicherTag(testvektor.termindatum)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment