简体   繁体   English

如何在 Java 单元测试中测试 Update 方法

[英]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.我正在为我的 ServiceImpl 方法创建单元测试,并尝试为update方法创建单元测试。 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 .它返回相应实体的DTO ,但我真的不知道是否应该使用@Spy@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).update()方法的返回类型更重要的是,我相信您应该测试传递给模拟存储库的save()方法的实体的值(我假设您有一个)。

For this, you can use ArgumentCaptor .为此,您可以使用ArgumentCaptor

For testing the return logic of your update() method, you can simply assign its result in your test to a response object.为了测试update()方法的返回逻辑,您可以简单地将其在测试中的结果分配给响应对象。 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):这是一个测试模拟存储库的 save 方法的参数和更新方法的返回的示例(它们应该是两个不同的单元测试):

@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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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