java - 传递给 argumentMatchers.eq() 的值显示为 null
问题描述
我正在嘲笑一个getQuizFromQuizResponse
需要非空对象的方法。我尝试使用eq()
ArgumentMatchers 库中的来传递我初始化的现有对象。当我在我的 IDE 中调试 SecurityServiceTest 时,eq(quizResponse)
它完全符合我的意图。当我在我正在模拟的方法中到达断点时,getQuizFromQuizResponse()
,quizResponse
然后显示为空。
为什么不eq()
保留我在模拟时定义的值?
进一步的上下文:
- [过时]我试图模拟的方法的类
QuizMapper
是间谍。我提到这一点是因为我觉得这可能就是我遇到这个问题的原因? - [过时] 我之所以
QuizMapper
成为间谍是因为我们没有将此类创建为 bean,因为它的目的是简单地从 DTO 映射到另一个......所以它不是服务。- [过时] 因为
QuizMapper
不是 bean,所以我无法自动装配它或将其注入SecurityService
并尝试使其成为一个@Mock
mockito 给我这样的错误:“Strict stubbing argument mismatch”
- [过时] 因为
- 我现在将 QuizMapper 作为一个
@Mock
,因为我意识到verifyUser()
我正在调用一个导致参数不匹配的构建器具有一些我认为不利于存根的初始化数据?这是错误:
org.mockito.exceptions.misusing.PotentialStubbingProblem:
Strict stubbing argument mismatch. Please check:
- this invocation of 'checkIdentity' method:
feignClient.checkIdentity(
0L,
null,
Verification(type=Initiate, settings=Settings(mode=null,
reference=Reference1, locale=en_US, venue=online), persons=.
[Person(name=null, addresses=[], ssn=null, phones=null,
emails=null, context=primary, Id=Id)], answers=null)
);
-> at com.*.Service.verifyUser(SecurityService.java:39)
- has following stubbing(s) with different arguments:
1. feignClient.checkIdentity(
0L,
null,
null
);
这是我的代码:
@ExtendWith(MockitoExtension.class)
class SecurityServiceTest {
@InjectMocks
private SecurityService securityService;
@Spy
private FeignClient feignClient;
@Spy
private QuizMapper quizMapper;
@Test
void testVerifyUserReturnsQuiz() throws ProviderException {
String id = "Id";
String mode = "test";
Quiz expectedQuiz = new Quiz();
expectedQuiz.setId("quizId");
Verification verification = new VerificationBuilder()
.setType(VerificationBuilder.TYPE)
.setMode(mode)
.setId(id)
.build();
QuizResponse quizResponse = new QuizResponse();
quizResponse.setProducts(new ProductTestUtil().getProductList());
given(
feignClient.checkIdentity(any(Long.class), any(String.class), eq(verification))
).willReturn(singleQuizResponse);
given(
quizMapper.getQuizFromQuizResponse(eq(quizResponse))
).willReturn(expectedQuiz);
Quiz actualQuiz = securityService.verifyUser(id);
assertEquals(actualQuiz, expectedQuiz);
}
}
这是我试图在单元测试中调用的类和函数
@Slf4j
@Service
public class SecurityService {
private final FeignClient feignClient;
@Value(“${mode}")
private String settingsMode;
@Value("${accountId}")
private long accountId;
@Value("${workflowName}")
private String workflowName;
public SecurityService(FeignCleint feignClient) {
this.feignClient = feignClient;
}
public Quiz verifyUser(String lexId) throws ProviderException {
QuizMapper quizMapper = new QuizMapper();
Verification user = new VerificationBuilder()
.setType(VerificationBuilder.TYPE)
.setMode(mode)
.setId(Id)
.build();
logger.info("Verification POST request: {}", user);
QuizResponse response = feignClient.checkIdentity(accountId, workflowName, user);
logger.info("QuizResponse POST response: {}", response);
return quizMapper.getQuizFromQuizResponse(response);
}
}
解决方案
推荐阅读
- python - 将大量图像保存为数组
- flutter - 颤振让孩子在列内匹配另一个孩子的宽度
- sql - 如何使用 SQL 为字符串中的每个字母切换大小写?
- javascript - 如何在 React 中将音频添加到鼓机?
- php - .htaccess 将电子邮件地址重定向到 URL 参数
- angular - 如何将表单控件值作为对象并在输入字段中呈现其标签?
- python - 我如何缩进这个 if 循环有问题吗?
- python - 使形状在 Pygame 中消失然后重新出现
- symfony - Symfony 4.3 - 无法在嵌套继承中自动装配服务参数
- python - Python selenium webdriver 没有从网页中看到所有表数据