[英]How to Unit Test A Method Argument which is a Builder Class in Mockito?
I have a dependency in the Spring Boot which is called from my class.我在 Spring Boot 中有一个依赖项,它是从我的类中调用的。 This dependency makes a call with a newly instantiated builder pattern class as an argument.
此依赖项使用新实例化的构建器模式类作为参数进行调用。 I need to know if it is possible to verify if this single builder class was constructed with certain values when the dependency call is made, eg:
我需要知道是否有可能在进行依赖调用时验证这个单个构建器类是否是用某些值构造的,例如:
public class MyClass {
@Autowired
private MyDep myDep;
public void callMyDepService() {
myDep.serviceCall(new MyBuilder().paramOne("One").paramTwo("Two").build());
}
}
As you can see, a new MyBuilder
is instantiated every time, so I can't just use Mockito's eq()
to match the arguments' value.如您所见,每次都会实例化一个新的
MyBuilder
,所以我不能只使用 Mockito 的eq()
来匹配参数的值。
I would like to make sure callMyDepService calls myDep.serviceCall with certain parameter values.
我想确保 callMyDepService 使用某些参数值调用 myDep.serviceCall。
Using a captor seems to be the easiest way to do that.使用俘虏似乎是最简单的方法。 Something like this, where
SomeClass
is the class your MyBuilder
builds.像这样,其中
SomeClass
是您的MyBuilder
构建的类。
@ExtendWith(MockitoExtension.class)
public class CaptureTest {
@InjectMocks
MyClass classUnderTest;
@Mock
MyDep myDep;
@Test
public void test() {
classUnderTest.callMyDepService();
Captor<SomeClass> captor = ArgumentCaptor.forClass(SomeClass.class);
Mockito.verify(myDep).serviceCall(captor.capture());
SomeClass parameter = captor.getValue();
// do assertions
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.