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

iyr7buue  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(118)

我发现自己处于不知道测试代码答案的情况下。我有一个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<String,String>,以便主IMPL方法可以给予我OK。

qpgpyjmq

qpgpyjmq1#

我不确定模拟是否正确启动。
尝试添加runwith注解。

@RunWith(MockitoJunitRunner.class)

字符串

在setup()方法中添加MockitoAnnotations.initMocks()
检查这个ans:https://stackoverflow.com/a/15494996

v09wglhw

v09wglhw2#

您的jwtServices不是一个mock,但您正在使用when进行mock。因此,它的所有混乱和出错。
总的来说,您的测试看起来并不像是设计正确的。通常,它应该是一个单独的类,测试它所有的依赖关系。你有一个奇怪的组合。

相关问题