简体   繁体   中英

How to test Update method in Java Unit Test

I am creating Unit Test for my ServiceImpl methods and I try to create a unit test for update method. However, I am not sure how should I test this method. It returns DTO of corresponding entity, but I have really no idea if I should use @Spy or @Captor . If I set a mock variable and then try to retrieve it and update its value, I will need to retrieve the same record to check its updated value.

I am new in testing and I have not found a proper example for update method. Any help would be appreciated.

public CompanyDTO update(CompanyRequest companyRequest, UUID uuid) {

    final Company company = companyRepository.findByUuid(uuid)
            .orElseThrow(() -> new EntityNotFoundException(COMPANY));
    company.setName(companyRequest.getName());
    final Company savedCompany = companyRepository.save(company);
    return new CompanyDTO(savedCompany);
}

Update: Finally I make it worked, but I am not sure for some parts. Is there anything missing or redundant in the following test method?

@InjectMocks
private CompanyServiceImpl companyService;

@Mock
private CompanyRepository companyRepository;

@Captor
ArgumentCaptor<Company> companyCaptorEntity;    

@Test
public void testUpdate() {
    final UUID uuid = UUID.randomUUID();
    final CompanyRequest request = new CompanyRequest();
    request.setName("Updated Company Name");

    final Company company = new Company();
    company.setName("Company Name");
    
    when(companyRepository.findByUuid(uuid))
        .thenReturn(Optional.ofNullable(company));

    //??? Do we need this?
    when(companyRepository.save(any())).thenReturn(company);

    CompanyDTO result = companyService.update(request, uuid);

    Mockito.verify(companyRepository).save(companyCaptor.capture());

    Company savedCompany = companyCaptor.getValue();
    assertEquals(request.getName(), savedCompany.getName());
}

More importantly than the return type of the update() method, I believe you should test the values of the entity passed to the save() method of your mock repository (I assume you have one).

For this, you can use ArgumentCaptor .

For testing the return logic of your update() method, you can simply assign its result in your test to a response object. You can then compare this object with the values of your input object.

Here is an example of both testing the arguments to the save method of your mock repository and the return of your update method (they should be two different unit tests):

@Test
void test() {
    // Arrange
    ArgumentCaptor<Entity> entityArgumentCaptor = ArgumentCaptor.forClass(Entity.class);
    InputDto inputDto = prepareTestInput();

    // Act
    ResponseDto responseDto = service.update(inputDto);
    verify(repositoryMock, times(1)).save(entityArgumentCaptor.capture());

    // Assert
    Entity savedEntity = argumentCaptor.getValue();
    assertEquals(input.getVariable1(), savedEntity.getVariable1());
    // .....
    // compare ResponseDto and InputDto too
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM