javaspring-bootrestspring-cloud-feign

someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called


I have a this unit test that fails when I have the following doNothing() for two calls inside the unit test

@Test
    void storeStockPersist() {
        Long idKey=1L;
        String name = "test";
        Map params = Map.of("idKey",idKey,"name",name);
        StoreStockPersistRequest storeStockPersistRequest = new StoreStockPersistRequest(1L, "Test", params);
        doNothing().when(planKeyService).persistStoreStockKey(storeStockPersistRequest);
        doNothing().when(actRestClient).persistStoreStockKey(storeStockPersistRequest);
        ResponseEntity<?> successfulCreate = planKeyController.storeStockPersist(storeStockPersistRequest);
        assertTrue(successfulCreate.getStatusCode() == HttpStatus.NO_CONTENT);
    }

It throws this exception

Only void methods can doNothing()! Example of correct use of doNothing(): doNothing(). doThrow(new RuntimeException()) .when(mock).someVoidMethod(); Above means: someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called org.mockito.exceptions.base.MockitoException: Only void methods can doNothing()! Example of correct use of doNothing(): doNothing(). doThrow(new RuntimeException()) .when(mock).someVoidMethod(); Above means: someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called at com.homedepot.kimsbff.controller.PlanKeyControllerTest.storeStockPersist(PlanKeyControllerTest.java:101)

This is the controller code and service code

public class PlanKeyController {

private final PlanKeyService planKeyService;


    public PlanKeyController(PlanKeyService planKeyService) {
        this.planKeyService = planKeyService;
    }

    public ResponseEntity<?> storeStockPersist(@RequestBody StoreStockPersistRequest storeStockPersistRequest) {
            planKeyService.persistStoreStockKey(storeStockPersistRequest);
            return ResponseEntity.noContent().build();
        }
}

here is the service class

 public class planKeyService{
private final ActRestClient actRestClient;
public PlanKeyServiceImpl(ActRestClient actRestClient) {
           this.actRestClient = actRestClient;
}
    public void persistStoreStockKey(StoreStockPersistRequest storeStockPersistRequest) {
            actRestClient.persistStoreStockKey(storeStockPersistRequest);
        }
}

Here is feign client

 @FeignClient(value = "kimsAct", url = "${internal-service.kims-act.url}", configuration = KimsActFeignConfig.class)
    public interface ActRestClient {
    
        ResponseEntity<?> persistStoreStockKey(@RequestBody StoreStockPersistRequest storeStockPersistRequest);
    }

Solution

  • Try to return null on mock!!

    doReturn(null).when(planKeyService).persistStoreStockKey(storeStockPersistRequest);
    doReturn(null).when(actRestClient).persistStoreStockKey(storeStockPersistRequest);
    

    The error say you only doNothing on "void" methods.

    Instead of "doNothing", try to return null(nothing) on your mock requests.