首页 > 解决方案 > 传递给 argumentMatchers.eq() 的值显示为 null

问题描述

我正在嘲笑一个getQuizFromQuizResponse需要非空对象的方法。我尝试使用eq()ArgumentMatchers 库中的来传递我初始化的现有对象。当我在我的 IDE 中调试 SecurityServiceTest 时,eq(quizResponse)它完全符合我的意图。当我在我正在模拟的方法中到达断点时,getQuizFromQuizResponse()quizResponse然后显示为空。

为什么不eq() 保留我在模拟时定义的值?

进一步的上下文:

    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);
    }
}

标签: javaunit-testingmockingmockitospy

解决方案


推荐阅读