![](/img/trans.png)
[英]Spring Webflux Mockito - mock the response of a Webclient call
[英]Mock a class type argument with ArgumentsMatchers and Mockito - Mongo aggregation Spring Webflux
我有这个对 MongoDB 的调用,我想模拟它。
private ReactiveMongoTemplate reactiveMongoTemplate;
...constructor
reactiveMongoTemplate.aggregate(aggregation, "foo_collection", FooData.class);
我试过这个,但总是得到一个NullPointerException
Mockito.when(reactiveMongoTemplate.aggregate(ArgumentMatchers.any(), ArgumentMatchers.anyString(), ArgumentMatchers.any()))
.thenReturn(Flux.just(fooData));
我也试过这个并得到同样的错误
Mockito.when(reactiveMongoTemplate.aggregate(ArgumentMatchers.any(), ArgumentMatchers.anyString(), ArgumentMatchers.<Class<FooData>>any())))
.thenReturn(Flux.just(fooData));
模拟似乎是正确的,您Mockito
在测试类中初始化了Mockito
?
public class TestClass {
@Before
public void initTest() {
MockitoAnnotations.initMocks(this);
}
//..
}
或者:
@RunWith(MockitoJUnitRunner.class)
public class TestClass {
//...
}
您是否在类测试中使用@Mock
注入ReactiveMongoTemplate
?
public class TestClass {
@Mock
private ReactiveMongoTemplate reactiveMongoTemplate;
//..
}
我使用这样的模拟找到了解决方案:
Mockito.when(reactiveMongoTemplate.aggregate(any(Aggregation.class), anyString(), eq(FooData.class)))
.thenReturn(Flux.just(fooData));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.