vendredi 23 novembre 2018

Writing j unit test

can some one help me write Junit coverage test for below class. The class is ASIAKSMASTERCONSUMER and the one you test asiaksConsuemrtest.java. Class, methods and line has zero percentage coverage so it would really be helpful if you guys could me with this thing to improve. Please right any suggestion or better the code for the coverage. Thank you

  package fi.mandatumlife.epeli.ms.personservice.core.service;

  import fi.mandatumlife.epeli.ext.asiakasmaster.api.adapter.AsiakasAdapter;

  import fi.mandatumlife.epeli.ext.asiakasmaster.api.dto.PartyByReferenceDTO;

  import fi.mandatumlife.epeli.ms.personservice.api.dto.PersonDto;

  import 
  fi.mandatumlife.epeli.ms.personservice.core.mapper.AsiakasPersonMapper;

  import java.util.Optional;

  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.http.HttpStatus;

  import org.springframework.stereotype.Service;

  import org.springframework.web.client.HttpServerErrorException;


 @Service

 public class AsiakasMasterConsumer {

 @Autowired

 private AsiakasAdapter asiakasAdapter;

 public PersonDto getAsiakasPerson(String reference, String insuranceNumber, 

 Integer versionNumber){

 PersonDto personDto = 

 AsiakasPersonMapper.toDto().apply(getAsiakasPersonPlain(reference));

 personDto.setInsuranceNumber(insuranceNumber);

 personDto.setVersionNumber(versionNumber);

 return personDto;

 }


 public PartyByReferenceDTO getAsiakasPersonPlain(String reference) {

 Optional<PartyByReferenceDTO> party = asiakasAdapter

    .getPartyByReference(reference, AsiakasAdapter.REFERENCE_VALUE);

 return party.orElseThrow(() -> new 

  HttpServerErrorException(HttpStatus.NOT_FOUND, "Person not found"));

 }

 }










  package fi.mandatumlife.epeli.ms.personservice.service;

  import static org.mockito.ArgumentMatchers.any;

   import com.fasterxml.jackson.databind.ObjectMapper;
   import fi.mandatumlife.epeli.ext.asiakasmaster.api.adapter.AsiakasAdapter;
   import 
    fi.mandatumlife.epeli.ext.asiakasmaster.api.dto.PartyByReferenceDTO;
   import fi.mandatumlife.epeli.ms.personservice.api.dto.PersonDto;
   import 
   fi.mandatumlife.epeli.ms.personservice.core.service.AsiakasMasterConsumer;
    import java.io.IOException;
    import java.time.LocalDateTime;
   import java.util.Optional;
   import javax.transaction.Transactional;
   import org.junit.Assert;
    import org.junit.Before;
   import org.junit.FixMethodOrder;
   import org.junit.Test;
   import org.junit.runner.RunWith;
   import org.junit.runners.MethodSorters; 
   import org.mockito.Mockito;
   import org.slf4j.Logger;
   import org.slf4j.LoggerFactory;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.boot.test.context.SpringBootTest;
   import org.springframework.boot.test.mock.mockito.MockBean;
   import org.springframework.core.io.ClassPathResource;
   import org.springframework.core.io.Resource;
   import org.springframework.test.annotation.Rollback;
   import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


   @RunWith(SpringJUnit4ClassRunner.class)
   @SpringBootTest(webEnvironment = 
   SpringBootTest.WebEnvironment.RANDOM_PORT)
   @Transactional
   @Rollback(false)
   @FixMethodOrder(MethodSorters.NAME_ASCENDING)
   public class AsiakasConsumerTest {

   static final Logger log = 
   LoggerFactory.getLogger(AsiakasConsumerTest.class);

   private static final String CREATOR = "epeliuser";
   private static final LocalDateTime CREATE_TIME = LocalDateTime.now();
   private static boolean setUpIsDone = false;


   @Autowired
   private AsiakasMasterConsumer service;

   @MockBean
   private AsiakasAdapter asiakasAdapter;

   @Autowired
   private ObjectMapper objectMapper;

   @Before
   public void setUp() {
   if (!setUpIsDone) {
   //      deleteData();
   setUpIsDone = true;
   }
   }

   @Test
   public void stage1_insertData() {

   createTestData();

   }

   //@Test
    public void stage2_getAsiakasPerson() {
   Resource resource = new ClassPathResource("personResponse.json");
   PartyByReferenceDTO partyByReferenceDTO = null;

   try {
   partyByReferenceDTO = objectMapper
      .readValue(resource.getFile(), PartyByReferenceDTO.class);
    } catch (IOException e) {
   e.printStackTrace();
    }

    Assert.assertNotNull("Marshaling of  test json failed", 
    partyByReferenceDTO);

    Mockito.when(asiakasAdapter.getPartyByReference(any(), any()))
    .thenReturn(Optional.of(partyByReferenceDTO));

    PersonDto person = service
        .getAsiakasPerson("SSN-test", "insuranceNumber", null);

     Assert.assertEquals("Kesä-T Zoey", person.getName());
     }

     @Test
    public void stageZ_clearTables()
     {
     //    this.deleteData();
      }


    private void createTestData() {

     }
     }

Aucun commentaire:

Enregistrer un commentaire