谁能给我一个例子,说明如何使用org.mockito.ArgumentCaptor类,以及它是如何不同于简单的匹配器提供的mockito。
我阅读了提供的模拟文档,但那些并没有说明清楚,他们都不能解释清楚。
谁能给我一个例子,说明如何使用org.mockito.ArgumentCaptor类,以及它是如何不同于简单的匹配器提供的mockito。
我阅读了提供的模拟文档,但那些并没有说明清楚,他们都不能解释清楚。
当前回答
我创建了这个例子来模拟一个非常简单的服务,它使用一个存储库来保存一个String(没有依赖注入,没有实体),只是为了快速地教授ArgumentCaptor。
服务接收、转换为大写并修饰名称,然后调用存储库。 存储库“保存”字符串。 使用ArgumentCaptor,我想知道哪个值被传递到存储库,然后检查它是否被修剪和大写,如预期的那样
3个类:PersonService, PersonRepository和PersonServiceTest(包省略)
public class PersonService {
private PersonRepository personRepository;
public void setPersonRepository(final PersonRepository personRepository) {
this.personRepository = personRepository;
}
public void savePerson(final String name) {
this.personRepository.save(name.toUpperCase().trim());
}
}
public class PersonRepository {
public void save(final String person) {
System.out.println(".. saving person ..");
}
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
class PersonServiceTest {
@Test
void testPersonService() {
// Create the repository mock
final PersonRepository personRepositoryMock = mock(PersonRepository.class);
// Create the service and set the repository mock
final PersonService personService = new PersonService();
personService.setPersonRepository(personRepositoryMock);
// Save a person
personService.savePerson("Mario ");
// Prepare an ArgumentCaptor to capture the value passed to repo.saveMethod
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
// Capture the argument passed in the unique method invocation
verify(personRepositoryMock, times(1)).save(captor.capture());
// Check if the captured value is the expected one
final String capturedParameter = captor.getValue();
assertEquals("MARIO", capturedParameter);
}
}
其他回答
两个主要的区别是:
当您捕获一个参数时,您可以对这个参数进行更详细的测试,并使用更明显的代码; ArgumentCaptor可以捕获不止一次。
为了说明后者,假设你有:
final ArgumentCaptor<Foo> captor = ArgumentCaptor.forClass(Foo.class);
verify(x, times(4)).someMethod(captor.capture()); // for instance
然后捕获器将允许您访问所有4个参数,然后您可以分别对它们执行断言。
实际上,这个或任何数量的参数,因为VerificationMode不限制于固定数量的调用;无论如何,如果你愿意,抓捕者会给你所有的权限。
这样做的另一个好处是,编写这样的测试要比实现自己的ArgumentMatchers容易得多——特别是如果您将mockito和assertj结合在一起的话。
哦,请考虑使用TestNG而不是JUnit。
我同意@fge说的,更多的结束。让我们看一个例子。 假设你有一个方法:
class A {
public void foo(OtherClass other) {
SomeData data = new SomeData("Some inner data");
other.doSomething(data);
}
}
现在如果你想检查内部数据,你可以使用捕获器:
// Create a mock of the OtherClass
OtherClass other = mock(OtherClass.class);
// Run the foo method with the mock
new A().foo(other);
// Capture the argument of the doSomething function
ArgumentCaptor<SomeData> captor = ArgumentCaptor.forClass(SomeData.class);
verify(other, times(1)).doSomething(captor.capture());
// Assert the argument
SomeData actual = captor.getValue();
assertEquals("Some inner data", actual.innerData);
进行全面检查的步骤如下:
首先,准备参数捕获器:
ArgumentCaptor<ArgumentClass> argumentCaptor = ArgumentCaptor.forClass(ArgumentClass.class);
其次,验证对依赖组件(被测试主体的合作者)的调用。
Times(1)是默认值,所以需要添加。
verify(dependentOnComponent, times(1)).method(argumentCaptor.capture());
第三,使用捕获器的getValue()将参数传递给协作器
ArgumentClass someArgument = messageCaptor.getValue();
第四,对断言使用someArgument
我创建了这个例子来模拟一个非常简单的服务,它使用一个存储库来保存一个String(没有依赖注入,没有实体),只是为了快速地教授ArgumentCaptor。
服务接收、转换为大写并修饰名称,然后调用存储库。 存储库“保存”字符串。 使用ArgumentCaptor,我想知道哪个值被传递到存储库,然后检查它是否被修剪和大写,如预期的那样
3个类:PersonService, PersonRepository和PersonServiceTest(包省略)
public class PersonService {
private PersonRepository personRepository;
public void setPersonRepository(final PersonRepository personRepository) {
this.personRepository = personRepository;
}
public void savePerson(final String name) {
this.personRepository.save(name.toUpperCase().trim());
}
}
public class PersonRepository {
public void save(final String person) {
System.out.println(".. saving person ..");
}
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
class PersonServiceTest {
@Test
void testPersonService() {
// Create the repository mock
final PersonRepository personRepositoryMock = mock(PersonRepository.class);
// Create the service and set the repository mock
final PersonService personService = new PersonService();
personService.setPersonRepository(personRepositoryMock);
// Save a person
personService.savePerson("Mario ");
// Prepare an ArgumentCaptor to capture the value passed to repo.saveMethod
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
// Capture the argument passed in the unique method invocation
verify(personRepositoryMock, times(1)).save(captor.capture());
// Check if the captured value is the expected one
final String capturedParameter = captor.getValue();
assertEquals("MARIO", capturedParameter);
}
}
你可能想要结合使用verify()和ArgumentCaptor来确保在测试中执行,并使用ArgumentCaptor来计算参数:
ArgumentCaptor<Document> argument = ArgumentCaptor.forClass(Document.class);
verify(reader).document(argument.capture());
assertEquals(*expected value here*, argument.getValue());
参数的值显然可以通过argument. getvalue()进行进一步的操作/检查或任何你想做的事情。