Man*_*anu 0 java hamcrest mockito
我正在使用Mockito和Hamcrest在Java中进行单元测试.
我经常使用Hamcrests hasSize断言某些集合具有一定的大小.几分钟前,我正在编写一个测试,我正在捕获一个List调用(替换名称):
public void someMethod(A someObject, List<B> list)
Run Code Online (Sandbox Code Playgroud)
考试:
@Test
public void test() {
// (...)
ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(someMock).someMethod(same(otherObject), captor.capture());
assertThat(captor.getValue().size(), is(2)); // This works
assertThat(captor.getValue(), hasSize(2)); // This gives a compile error
// TODO more asserts on the list
}
Run Code Online (Sandbox Code Playgroud)
问题:
测试运行绿色与第一个assertThat,并且可能有其他方法来解决这个问题(例如,实现ArgumentMatcher<List>),但因为我总是使用hasSize,我想知道如何解决这个编译错误:
The method assertThat(T, Matcher<? super T>) in the type MatcherAssert is not applicable for the arguments (List, Matcher<Collection<? extends Object>>)
Run Code Online (Sandbox Code Playgroud)
解决此问题的一种方法是使用以下方法定义捕获者mockito annotations:
@RunWith(MockitoJUnitRunner.class)
public class MyTestClass {
@Captor
private ArgumentCaptor<List<B>> captor; //No initialisation here, will be initialized automatically
@Test
public testMethod() {
//Testing...
verify(someMock).someMethod(same(otherObject), captor.capture());
assertThat(captor.getValue(), hasSize(2));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
720 次 |
| 最近记录: |