如何使用 Junit 和 Mockito 进行测试而不返回 null

问题描述 投票:0回答:2

我发现自己处于不知道测试代码答案的情况。 我有一个 Impl,它有一个寻找患者的方法,但在该方法内,我有另一个外部类,它调用一个方法,该方法还调用一个接口来接收响应。 我公开我的代码

IMPL

@Autowired
    private JwtServices jwtServices;
@Override
    public Map<String, Object> findByPatient(String identificationNumber, String birthdate, Boolean allowPatientData)  {

        Map<String, String> jwtData = jwtServices.getDecoderJwt();

        Map<String, String> dataPatient =  new HashMap<>();
        dataPatient.put("identificationNumber", identificationNumber);
        dataPatient.put("birthdate",birthdate);
        dataPatient.put("allowPatientData",allowPatientData.toString());
        dataPatient.put("appointmentId",jwtData.get("sub"));

        return this.apiFaroConfig.findPatient(dataPatient);
    }

我的代码 JwtServices:

 @Autowired
    private JWTDecoder jwtDecoder;

    public Map<String, String> getDecoderJwt(){
        Map<String, String> jwtData = new HashMap<>();

        Payload payloadHeaderAuthorization = jwtDecoder.getPayloadAuthorization();

        jwtData.put("iss", payloadHeaderAuthorization.getIss());
        jwtData.put("sub",  payloadHeaderAuthorization.getSub());

        return jwtData;
    }

我的测试 IMPL:

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class PatientServicesImplTest {

    @Mock
    private FaroApiRest apiFaroRest;

    @InjectMocks
    private JwtServices jwtServices ;

    @Mock
    private JWTDecoder jwtDecoder;

    @Mock
    private Payload payload;

    @InjectMocks
    private PatientServicesImpl patientServices;

    //Initialize variable
    private Map<String, String> dataPatient;
    private Map<String, Object> dataResponse;
    private Map<String, String> jwtData ;

    @BeforeEach
    void setUp() {
        //Initialize instances
        patientServices = new PatientServicesImpl(apiFaroRest);
        dataPatient = new HashMap<>();
        dataResponse = new HashMap<>();
        jwtData = new HashMap<>();

        //initialize dataPatient
        dataPatient.put("identificationNumber", "XXXX");
        dataPatient.put("birthdate","XXXX");
        dataPatient.put("allowPatientData", "true");
        dataPatient.put("appointmentId","XXX");

        //Initialize dataResponse status->200 ok
        dataResponse.put("datosPersonales", "XXX");
        dataResponse.put("anamnesis", "XXX");
        dataResponse.put("gdpr", "XXX");

        //Initialize data jwt
        jwtData.put("iss","560");
        jwtData.put("sub", "123456");

    }

    @Test
    void findByPatient() {
        //when

        //Jwt Services
        when(jwtDecoder.getPayloadAuthorization()).thenReturn(payload);

        when(jwtServices.getDecoderJwt()).thenReturn(jwtData);

        //PatientsImpl
        when(patientServices.findByPatient("XXXX", "XXXX", true)).thenReturn(dataResponse);
        when(apiFaroRest.findPatient(dataPatient)).thenReturn(dataResponse);

        //given
        Map<String, Object> response = apiFaroRest.findPatient(dataPatient);

        //then
        assertTrue(response.containsKey("datosPersonales"));
        assertNotNull(response);
       }

}

错误:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
HashMap cannot be returned by getSub()
getSub() should return String
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
   Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
   - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

我不明白发生了什么事。 我不明白为什么当我指定其回报时会得到回报更改。 然后,如果我删除 jwtDecoder,它会返回 null jwtService,因为它尝试调用 jwtDecoder 方法,但它不“存在”,但当然,如果我想要的是 JwtServices 检查其 getDecoder 方法,然后我将其返回 Map这样主要的 IMPL 方法就可以给我确定了。

java mockito junit5
2个回答
0
投票

我不确定模拟是否正确启动。

尝试添加 runwith 注释。

@RunWith(MockitoJunitRunner.class)

在 setup() 方法中添加

MockitoAnnotations.initMocks()

检查这个:https://stackoverflow.com/a/15494996


0
投票

你的

jwtServices
不是嘲笑,但你是在用
when
嘲笑它。因此,一切都变得混乱并出错了。

总体而言,您的测试看起来设计不正确。通常它应该是被测试的单个类,其所有依赖项都被模拟。你的组合很奇怪。

© www.soinside.com 2019 - 2024. All rights reserved.