hamcrestã®Matcherã¡ã¢
æè¡ãã¿ãããªãã¨ããã§çãä¸ãã¦ãã¾ã£ããæè¡ãã¿ããããæè¡ãã¿ã
ãã¦ãJUnitã使ãéãhamcrestã©ã¤ãã©ãªã使ã£ã¦ãè±èªã¨ãã¦èªãããããªassertionãæ¸ãããªãã¦ã®ã¯æµè¡ã£ã¦ããæµè¡ã£ã¦ããªãã£ããï¼
JUnit4éå®ã ããã©ãassertionã®éãassertEqualsã¨ãè²ã assertionã®ã¡ã½ããã¯ãããã©ãå ¨ã¦assertThatã§æ¸ããã¨ãã§ããã¯ããassertThatã¡ã½ããã®ç¬¬ä¸å¼æ°ã«ãã¹ã対象ã第äºå¼æ°ã«hamcrestã®Matcherã¤ã³ã¿ã¼ãã§ã¤ã¹ã®å®è£ ãä¸ãã¾ãããªãã®ãã£ã¡ãã§ããã
Jiemamyã§ã¯ããªãã¹ãassertThat以å¤ã®assertionã¡ã½ããã使ããªãããã«ãã¹ããæ¸ãã¦ãã¾ããï¼ããããããããä¸ã¤ãæ®ã£ã¦ãªããããï¼
ã¾ãã以ä¸ã®ããã«æ¸ãã¨ãè±èªã£ã½ãã®ãæ¸ãã¾ãããã¨ã
assertThat(aaaa, is(not(equalTo(bbbb))));
ã«ãã³ã¨ã«ã³ããå
¨é¨åãé¤ã㨠assert that aaaa is not equal to bbbb. ã¨ãªããè±èªã¨ãã¦èªãããããaaaaã¯bbbbã¨ã¯ç°ãªããã¨ãã表æã§ãããããããã¨ã«ãã£ã¦ãã¾ãã«ã³ã¼ãèªèº«ãã³ã¡ã³ãã®ãããªå½¹å²ãæããã¾ãã
ãã®ä»è²ã
ãªãã¿ã¼ã³ãããã®ã§ãçã£ç«¯ããã¾ã¨ãã¦ã¿ã¾ãããã¡ãªã¿ã«ã以ä¸ã®assertionã¯ãå
¨ã¦ééããããã«æ¸ãã¦ããã¾ããããããã®è§£èª¬ã¯ãã¾ããããä¸è¨ã®ããã«èªãã§ã¿ãã¨ãä½ãassertãããã®ãèªã¿åããã¨æãã¾ããstaticã¤ã³ãã¼ãã䏿ã使ã£ã¦ããã®ã«ã注ç®ã§ããã
import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.sameInstance; import static org.hamcrest.Matchers.closeTo; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.endsWith; import static org.hamcrest.Matchers.equalToIgnoringCase; import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace; import static org.hamcrest.Matchers.eventFrom; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.hasProperty; import static org.hamcrest.Matchers.hasToString; import static org.hamcrest.Matchers.hasXPath; import static org.hamcrest.Matchers.isIn; import static org.hamcrest.Matchers.isOneOf; import static org.hamcrest.Matchers.lessThan; import static org.hamcrest.Matchers.lessThanOrEqualTo; import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.typeCompatibleWith; import static org.junit.Assert.assertThat; import java.io.ByteArrayInputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.EventObject; import java.util.HashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.Diff; import org.hamcrest.Matcher; import org.hamcrest.Matchers; import org.junit.Test; import org.w3c.dom.Document; public class HamcrestTest { @Test public void testHamcrestBasic() { String s = "foo"; String s2 = s; String s3 = "foobarbaz"; String s4 = "foo bar baz"; String n = null; Class<?> c = String.class; // --- åºæ¬å½¢ assertThat(n, is(nullValue())); // assert that n is null value. assertThat(s, is(notNullValue())); // assert that s is not null value. assertThat(s, is("foo")); // asser that s is "foo". assertThat(s, is(not(s3))); // assert that s is not f3. assertThat(s, is(equalTo(s2))); // assert that s is equal to f2. [equals()ã«ããæ¯è¼] assertThat(s, is(not(equalTo(s3)))); assertThat(s, is(sameInstance(s2))); // assert that s is same instance of f2. [== ã«ããæ¯è¼] assertThat(s, is(not(sameInstance(s3)))); assertThat(s, is(instanceOf(String.class))); // assert that s is instance of String. assertThat(c, is(typeCompatibleWith(CharSequence.class))); // assert that c is type (which is) compatible with CharSequence class assertThat(c, is(not(typeCompatibleWith(Number.class)))); // --- Stringç³» assertThat(s, is(equalToIgnoringCase("FOO"))); assertThat(s4, is(equalToIgnoringWhiteSpace("foo bar baz"))); assertThat(s3, startsWith("foo")); assertThat(s3, endsWith("baz")); assertThat(s3, containsString("bar")); // --- æ°å¤ç³» double num = 1.0; assertThat(num, is(greaterThan(0.5))); // 1.0 > 0.5 assertThat(num, is(greaterThanOrEqualTo(1.0))); // 1.0 >= 1.0 assertThat(num, is(lessThan(1.1))); // 1.0 < 1.1 assertThat(num, is(lessThanOrEqualTo(1.0))); // 1.0 <= 1.0 assertThat(num, is(closeTo(0.95, 0.1))); // 1.0 = 0.95±0.1 assertThat(num, is(not(closeTo(0.95, 0.01)))); // 1.0 != 0.95±0.01 } @Test public void testHamcrestCollection() { String f = "foo"; String[] a = { "foo", "foobar", "foobarbaz" }; Collection<String> c = Arrays.asList("foo", "bar", "baz"); Map<String,String> map = new HashMap<String, String>(); map.put("A", "a"); map.put("B", "b"); // --- ã³ã¬ã¯ã·ã§ã³ç³» assertThat(c, hasItems("bar", "baz")); assertThat(f, isIn(c)); assertThat(f, isOneOf(a)); assertThat(map, hasEntry("A", "a")); assertThat(map, not(hasEntry("A", "b"))); assertThat(map, not(hasEntry("Z", "z"))); assertThat(map, Matchers.<String, String>hasKey("A")); assertThat(map, not(Matchers.<String, String>hasKey("Z"))); assertThat(map, Matchers.<String, String>hasValue("a")); assertThat(map, not(Matchers.<String, String>hasValue("z"))); } @Test public void testHamcrestEvent() throws Exception { Object o = new Object(); EventObject ev = new EventObject(o); Object o2 = new Object(); EventObject ev2 = new EventObject(o2); assertThat(ev, is(eventFrom(o))); assertThat(ev, is(not(eventFrom(o2)))); assertThat(ev2, is(eventFrom(o2))); assertThat(ev2, is(not(eventFrom(o)))); } @Test public void testHamcrestXml() throws Exception { final String XML1 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +"<foo hoge=\"hoge\" fuga=\"fuga\">\n" +" <bar>baz</bar>\n" +"</foo>\n"; final String XML2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +"<foo fuga=\"fuga\" hoge=\"hoge\">\n" +" <bar>baz</bar>\n" +"</foo>\n"; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse( new ByteArrayInputStream(XML1.getBytes("UTF-8"))); assertThat(document, hasXPath("/foo/bar", is("baz"))); // hamcrestãããªãã¦ãxmlunitããç´¹ä»ã便å©ã ã£ãã®ã§ã¤ãã§ã«ã DetailedDiff diff = new DetailedDiff(new Diff(XML1, XML2)); assertThat(diff.getAllDifferences().toString(), diff.similar(), is(true)); } @Test public void testHamcrestBean() throws Exception { Hoge hoge = new Hoge(); assertThat(hoge, hasProperty("fuga")); assertThat(hoge, not(hasProperty("ponyo"))); hoge.setFuga("wooo"); assertThat(hoge, hasToString(equalTo("Hoge[wooo]"))); // âã¯âã¨åã assertThat(hoge.toString(), is(equalTo("Hoge[wooo]"))); } @Test public void testHamcrestLogical() { String f1 = "foobarbaz"; String f2 = "foo bar baz"; Collection<Matcher<? extends String>> matchers = new ArrayList<Matcher<? extends String>>(); matchers.add(containsString("foo")); matchers.add(containsString("oob")); matchers.add(containsString("arb")); // f1ã¯ãã¹ã¦ãæºãã assertThat(f1, is(allOf(matchers))); // f2ã¯ä½ããä¸ã¤ãæºãã assertThat(f2, is(anyOf(matchers))); } private class Hoge { private String fuga; public String getFuga() { return fuga; } public void setFuga(String fuga) { this.fuga = fuga; } @Override public String toString() { return "Hoge[" + fuga + "]"; } } }
追è¨
Matcher ã® static import 㯠* ã§ããã¨æããã ãªããã¹ãã³ã¼ãã ãã
はてなブックマーク - Yamashiro0217のブックマーク / 2009年7月10日
ã£ã¦ããããã³ãããã£ããã©ãããã«ã¯çç±ããããã¨ãã£ã¦ããæ£ç¢ºãªçç±ã¯å¿ãã¦ãã¾ã£ãï½
ããããJiemamyã§ã¯static importã«ã¤ãã¦ã*ãç¦æ¢ãã¦ããã®ã¯ã確ããmockitoã®static importã¨ä½µç¨ããæã«èå¥ååã«ãããèµ·ãããã ã£ãã¨æããã¾ãããã¹ãã ãã*ã§ãããã£ã¦ã®ã¯ããããOKãªèãæ¹ã ã¨ã¯æããã©ãè¤æ°ã®ä»çµã¿ãçµã¿åãããæã«ããããåé¡ãèµ·ãããããããªãããã£ã¦ãã¨ã§ã