简体   繁体   中英

Mocking a void method in the same class that is under test with mockito and junit5?

i want to mock a void method in the same class that is under test with mockito. i can do that for not void method with annotating test class with @Spy and then use below code to return data i want.

        willAnswer(arg -> arg.getArgument(0))
            .given(customerService)
            .saveCustomer(any(Customer.class));

but how can do that for void method. (i already try willDoNothing().given() function)

this is my real method:

public void deleteCustomersByTenantId(TenantId tenantId) throws ThingsboardException {
    log.trace("Executing deleteCustomersByTenantId, tenantId [{}]", tenantId);
    Validator.validateId(tenantId, "Incorrect tenantId " + tenantId);
    customersByTenantRemover.removeEntities(tenantId, tenantId);
}

public void deleteCustomer(TenantId tenantId, CustomerId customerId) throws ThingsboardException {
    log.trace("Executing deleteCustomer [{}]", customerId);
    Validator.validateId(customerId, INCORRECT_CUSTOMER_ID + customerId);
    Customer customer = findCustomerById(tenantId, customerId);
    if (customer == null) {
        throw new IncorrectParameterException("Unable to delete non-existent customer.");
    }
    entityViewService.unassignCustomerEntityViews(customer.getTenantId(), customerId);
    assetService.unassignCustomerAssets(customer.getTenantId(), customerId);
    userService.deleteCustomerUsers(customer.getTenantId(), customerId);
    deleteEntityRelations(tenantId, customerId);
    entityGroupDao.deleteEntityGroupsByTenantIdAndCustomerId(customer.getTenantId(), customerId);
    customerDao.removeById(tenantId, customerId.getId());
}

private final PaginatedRemover<TenantId, Customer> customersByTenantRemover =
        new PaginatedRemover<>() {

            @Override
            protected PageData<Customer> findEntities(TenantId tenantId, TenantId id, PageLink pageLink) {
                return customerDao.findCustomersByTenantId(id.getId(), pageLink);
            }

            @Override
            protected void removeEntity(TenantId tenantId, Customer entity) throws ThingsboardException {
                deleteCustomer(tenantId, new CustomerId(entity.getUuidId()));
            }
        };

and this is the test:

void deleteCustomersByTenantId() throws ThingsboardException {
    //given
    Customer customer = new Customer();
    customer.setId(CUSTOMER_ID);
    customer.setTenantId(TENANT_ID);

    ArgumentCaptor<CustomerId> customerIdArgumentCaptor = ArgumentCaptor.forClass(CustomerId.class);

    var pageData = new PageData<>(Collections.singletonList(customer), 1 ,1 , false);

    given(customerDao.findCustomersByTenantId(any(UUID.class), any(PageLink.class)))
            .willReturn(pageData);
    doNothing()
            .when(customerService)
            .deleteCustomer(any(), any());

    //when
    customerService.deleteCustomersByTenantId(TENANT_ID);

    //then
    then(customerDao)
            .should(times(1))
            .findCustomersByTenantId(any(UUID.class), any(PageLink.class));
    then(customerService)
            .should(times(1))
            .deleteCustomer(any(TenantId.class), customerIdArgumentCaptor.capture());
    assertEquals(CUSTOMER_ID, customerIdArgumentCaptor.getValue());
}

every dependency is mock with @Mock.

Do nothing works for me. Just pay attention to the calling order

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;

doNothing().when(yourMock).yourMethod(yourArgs);

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