mardi 27 août 2019

Testsing completableFuture and lambdas

I am working the CompletableFuture class from java 8 and I have to test my code, but I'm struggling, because I don't know how can I test this methods, also I have to test the lambdas that I have used. Now I am using the .join method to block the execution, the problem is that the test don't recognize the lambda variables.

My code looks like this:

        public CompletableFuture<RegistrationInfoDTO> getRegisterInfo(InfoGuestDTO infoInvitedRequest) {
        InvitedLookUpRequest invitedLookUpRequest = generalInfoInvitedToLookUp(infoInvitedRequest);

        return getInfoInvitedService(invitedLookUpRequest)
            .thenCompose(infoRegistration -> findInitialInformation(infoRegistration.getBody(), infoInvitedRequest));
        }

And this is the test that I implemented:

        @Test
        public void findInvitedInfo() {
        HttpHeaders headers = getTestHttpHeaders();
        String mockURL = "url";
        HttpEntity<SeaPassInfoDTO> requestEntity = new HttpEntity<>(seaPassRequestTest, headers);

        given(kidRepository.existsById(anyString())).willReturn(true);
        when(lookUpConfig.getSystem()).thenReturn("KidsClub");
        when(lookUpConfig.getUrl()).thenReturn("anyURL");
        when(restTemplate.exchange(mockURL, HttpMethod.POST, requestEntity, GuestResponse.class))
                .thenReturn(new ResponseEntity<GuestResponse>(guestTest, HttpStatus.OK));

        RegistrationInfoDTO seaPassRegistrationInfo =
                guestGuestLookUpService.getRegistrationInformation(infoGuestDTO).join();

        assertNotNull(seaPassRegistrationInfo);
    }

When I run the test, I get an error because the infoRegistration variable in the

.thenCompose(infoRegistration -> findInitialInformation(infoRegistration.getBody(), infoInvitedRequest));

I wanted to know how can I test this type of methods with lambdas and asynchronous operations. Any advice?. Thanks.

Aucun commentaire:

Enregistrer un commentaire