如何解决Micronaut 测试 Mockito“此处检测到错误放置或误用的参数匹配器”
使用 Micronaut Test 和 Mockito,我正在尝试模拟 Azure Function 的协作类。
我相信我已经遵循了 Micronaut JUnit 5 和 Mockito 指令(尽管我使用的是构造函数注入),但是当我在模拟时尝试使用参数匹配器时,我遇到了以下问题:
io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type [com.example.org.SomeService]:
Misplaced or misused argument matcher detected here:
-> at com.example.org.SomeTest.wellTestSomething(SomeTest.java:41)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
代码看起来像:
@MicronautTest
final class SomeTest {
{
private final MyFunction myFunction;
private final SomeService someService;
CreateUserFunctionTest(final SomeService someService) {
myFunction = new MyFunction(someService);
this.someService = someService;
}
@Test
void aTest() {
when(someService.create(any(Thing.class))).thenReturn(Maybe.just(new Value()));
//rest of test
}
@MockBean(SomeServiceImpl.class)
SomeService someService() {
return mock(SomeService.class);
}
}
如果我用一个值替换参数匹配器它工作正常,所以我至少在一个层面上有信心所有注入都按预期工作。
是否可以在 Micronaut 测试中使用参数匹配器?如果是这样,我错过了什么?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。