Show Menu
Cheatography

JUnit and Hamcrest Common Functions Cheat Sheet (DRAFT) by

List of functions that are often used

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Annota­tions

@After
@After­Class
@Before
@Befor­eClass
@Test
@Test(­exp­ect­ed=­Exc­ept­ion.class)
@Test(­tim­eou­t=m­ili­sec­onds)

JUnit Asserts

assert­Arr­ayE­quals
assert­Equals
assert­Not­Equals
assert­False
assertTrue
assertNull
assert­NotNull
assertSame
assert­NotSame
fail
Assert methods optionally take an error string as the first parameter, followed by expected and actual values.

Hamcrest Assert

Hamcrest asserts take the form
assert­Tha­t(msg, actual, matcher)


Example:
assert­Tha­t("U­nau­tho­rized User", author­ize­dUser, hasKey­(us­erN­ame))

Core Matchers

allOf, anyOf
and, both, either, or
contai­nsS­tring, endsWith, startsWith
equal
hasKey
hasItem, hasItems
not
nullValue
sameIn­stance
These are the matchers which are included in JUnit 4

Number Matchers

closeTo
equalTo
notEqualTo
 

Array Matchers

arrayC­ont­aining
arrayC­ont­ain­ing­InA­nyOrder
arrayW­ithSize
emptyArray

Collection Matchers

contains
contai­nsI­nAn­yOrder
empty
emptyC­oll­ect­ionOf
emptyI­ter­ableOf
hasEntry
hasKey
hasValue
isIn
isOneOf

Bean Matchers

hasPro­perty
hasPro­per­tyW­ith­Value
samePr­ope­rty­ValueAs

Object Matchers

hasToS­tring
typeCo­mpa­tib­leWith

String Matchers

equalTo
equalT­oIg­nor­eCase
equalT­oIg­nor­eWh­ite­Space
isEmpt­yOr­Nul­lString
isEmpt­yString
string­Con­tai­nsI­nOrder