Skip to content

Commit dbf5d3d

Browse files
author
neildunn
committed
Added integration point with Java's assert keyword
1 parent 32e2c2e commit dbf5d3d

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.hamcrest;
2+
3+
/**
4+
* Integration method for use with Java's <code>assert</code> keyword.
5+
* Example:
6+
* <pre>
7+
* assert that("Foo", startsWith("f"));
8+
* </pre>
9+
*
10+
* @author Neil Dunn
11+
*/
12+
public class JavaLangMatcherAssert {
13+
private JavaLangMatcherAssert() {};
14+
15+
public static <T> boolean that(T argument, Matcher<? super T> matcher) {
16+
return matcher.matches(argument);
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.hamcrest;
2+
3+
import static org.hamcrest.JavaLangMatcherAssert.that;
4+
import static org.hamcrest.text.StringStartsWith.startsWith;
5+
import junit.framework.TestCase;
6+
7+
public class JavaLangMatcherAssertTest extends TestCase {
8+
9+
public void testFailingAssertion() throws Exception {
10+
boolean thrown;
11+
try {
12+
assert that("Foo", startsWith("Bar"));
13+
// Can't fail() here since catch block will catch JUnit exception.
14+
thrown = false;
15+
} catch (AssertionError expected) {
16+
thrown = true;
17+
}
18+
if (thrown) {
19+
fail();
20+
}
21+
}
22+
23+
public void testPassingAssertion() throws Exception {
24+
assert that("Foo", startsWith("Far"));
25+
}
26+
}

0 commit comments

Comments
 (0)