Java 使用ArgumentCaptor<;列表>;还有哈姆克雷斯特
我使用Mockito和Hamcrest在Java中进行单元测试 我经常使用HamcrestsJava 使用ArgumentCaptor<;列表>;还有哈姆克雷斯特,java,mockito,hamcrest,Java,Mockito,Hamcrest,我使用Mockito和Hamcrest在Java中进行单元测试 我经常使用HamcrestshasSize来断言某些集合具有一定的大小。几分钟前,我正在编写一个测试,其中捕获了调用(替换名称)的列表: 我自己找到了一个可能的解决办法: assertThat((List<B>) captor.getValue(), hasSize(2)); assertThat((List)captor.getValue(),hasSize(2)); 我自己找到了一个可能的解决方案: assertT
hasSize
来断言某些集合具有一定的大小。几分钟前,我正在编写一个测试,其中捕获了调用(替换名称)的列表
:
我自己找到了一个可能的解决办法:
assertThat((List<B>) captor.getValue(), hasSize(2));
assertThat((List)captor.getValue(),hasSize(2));
我自己找到了一个可能的解决方案:
assertThat((List<B>) captor.getValue(), hasSize(2));
assertThat((List)captor.getValue(),hasSize(2));
解决此问题的一种方法是使用mockito注释定义捕获者,如:
@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));
}
}
@RunWith(MockitoJUnitRunner.class)
公共类MyTestClass{
@俘虏
private ArgumentCaptor captor;//此处没有初始化,将自动初始化
@试验
公共测试方法(){
//测试。。。
验证(someMock.someMethod)(相同(其他对象),captor.capture());
资产(captor.getValue(),hasSize(2));
}
}
解决此问题的一种方法是使用mockito注释定义捕获者,如:
@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));
}
}
@RunWith(MockitoJUnitRunner.class)
公共类MyTestClass{
@俘虏
private ArgumentCaptor captor;//此处没有初始化,将自动初始化
@试验
公共测试方法(){
//测试。。。
验证(someMock.someMethod)(相同(其他对象),captor.capture());
资产(captor.getValue(),hasSize(2));
}
}
@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));
}
}