我刚刚看过hamcrest 1.2中org.hamcrest.core.CombinableMatcher上的'both'和'and'方法
出于某种原因,我无法得到以下内容进行编译
@Test
public void testBoth() {
String HELLO = "hello";
String THERE = "there";
assertThat("hello there", both(containsString(HELLO)).and(containsString(THERE)));
}
Run Code Online (Sandbox Code Playgroud)
我得到的编译信息是
and(org.hamcrest.Matcher<? super java.lang.Object>) in org.hamcrest.core.CombinableMatcher<java.lang.Object> cannot be applied to (org.hamcrest.Matcher<java.lang.String>)
Run Code Online (Sandbox Code Playgroud)
如果我为方法指定了类型参数explicity,它就可以工作
@Test
public void testBoth() {
String HELLO = "hello";
String THERE = "there";
Assert.assertThat("hello there", CombinableMatcher.<String>
both(containsString(HELLO)).and(containsString(THERE)));
}
Run Code Online (Sandbox Code Playgroud)
虽然这不是很好.
任何人都可以告诉我为什么编译器无法弄清楚这里的类型?在这种情况下,我无法相信这是预期的行为.
谢谢!