-1
  **Test Class**
     @RunWith(SpringRunner.class)
        //@WebMvcTest
        @AutoConfigureMockMvc
        @TestPropertySource(locations = "classpath:application-test.properties")
        @SpringBootTest(webEnvironment = WebEnvironment.MOCK)
        public class UnitTestingMockitoApplicationTe
 {

  private static @Autowired
Product PRODUCT = new privateProduct(110, static"WXYZ", MockMvc150d, mockMvc;"V20");

    @MockBean
public static final String privateURL ProductRepository= productRepository;"http://localhost:%s/product/";

  //  @Autowired
public static final Integer privatePRODUCT_ID IProductService= productService;101;

    @Test@Autowired
    public voidprivate contextLoads()TestRestTemplate {restTemplate;

    }
@LocalServerPort
    @Test
 private int port;

 public void testProductDelete_02() {
//   1. POST Method doThrow(newCase PersistenceException("ExceptionFrom Occured..!!")).when(productRepository).deleteById(productIdDB);
        verify(productRepository).deleteById(any());@Test
    }
**Service Class**

 @Service
public classvoid IProductServiceImpltestProductSave_01() implementsthrows IProductServiceException {
 
    @Autowired
    private ProductRepository repo;

   ProductDto productDTO = @OverridegivenProductDto();
        publicString voidformat deleteProduct= String.format(IntegerURL+"register", prodIdport) {;
        HttpHeaders httpHeaders = new repo.deleteByIdHttpHeaders(prodId);
        }
}
**Controller Class**


 
@RestController
@RequestMappinghttpHeaders.set(""Content-Type", "application/product"json;charset=UTF-8")
public class EmployeeRestController {

    @Autowired;
    private IProductService service;
@DeleteMapping("/delete/{id}")
  HttpEntity<ProductDto> httpEntity public= ResponseEntity<?>new deleteProductHttpEntity<ProductDto>(@PathVariable IntegerproductDTO, idhttpHeaders) {;
        ResponseEntity<?>ResponseEntity<ProductDto> response = null;
        boolean exist = servicerestTemplate.isProductExistexchange(id);
    format, HttpMethod.POST, httpEntity,
  if (exist) {
            serviceProductDto.deleteProduct(idclass);
            assertEquals(response = new ResponseEntity<String>.getStatusCode(id + "-Removed"), HttpStatus.OK);
        } else {
            response = new ResponseEntity<String>assertNotNull("Product NOT FOUND", HttpStatusresponse.BAD_REQUESTgetBody());
        }
        return response;
    }

Exception org.springframework.web.client.RestClientException: mockito wanted but not invoked, Actually there were zero interactions with this mock OccuringError while extracting response for type [class com.junittesting.dto.ProductDto] While calling this method from JPA Repository. I am getting mockito wantedm trying to write unit test cases but not invoked. Actually there were zero interactions with this mock at verify call.giving error

  • You are not testing any object instances/methods in your test (missing "when" block in terms of "given-when-then", even if `doThrow` was uncommented). What method are you trying to test? Can you show the code of the said method? – Jonasz Jan 14 '23 at 06:45
  • I want to use deleteById() method so how can we use which in Jpa repository and returns void – Ankush Bandu Kamble Jan 14 '23 at 06:47
  • Are you using the `deleteById` method in your service class? If so, attach the code showing that. Additionally - what do you want to test here? – Jonasz Jan 14 '23 at 07:08
  • 1
    Your test method is empty, aside from calling `verify` on your mock. – tgdavies Jan 14 '23 at 07:50
  • The only thing your test does is to check that `deleteById` got called. It doesn't do anything else first; so it's no surprise that the result is that `deleteById` didn't get called. You're going to have to add something to your test to make it actually call the method you're trying to test, which presumably is `deleteByProduct` in your `EmployeeRestController` class. – Dawood ibn Kareem Jan 14 '23 at 09:27
  • Your test does not call `deleteByProduct`, so there's no way `deleteById` could have been called. – knittl Jan 14 '23 at 12:28
  • @DawoodibnKareem could you plz help how to solve then – Ankush Bandu Kamble Jan 14 '23 at 13:34
  • @knittl please elaborate i m unable to solve – Ankush Bandu Kamble Jan 14 '23 at 13:34
  • @AnkushBanduKamble you are _not_ calling your method. You solve by calling your method. – knittl Jan 14 '23 at 14:05
  • @AnkushBanduKamble The solution is that you have to call the method that you're testing. Methods don't call themselves! subasri_ has shown you exactly what to do, in their answer. – Dawood ibn Kareem Jan 14 '23 at 21:03
  • @DawoodibnKareem yes got it but i m facing another issue can you elaborate it, i have mentioned above – Ankush Bandu Kamble Jan 15 '23 at 02:22
  • So if you have a completely new question, I would suggest you write a completely new question. You absolutely should NOT change your question after you've got an answer; because it invalidates the work that @subasri_ did to answer your question, and makes a mockery of the whole Stack Overflow model of questions and answers. Please change this question back to the one that subasri_ answered. – Dawood ibn Kareem Jan 15 '23 at 20:24
  • @subasri_ sorry brother, I have changed it to original one____@DawoodibnKareem Done Changed to orginal one – Ankush Bandu Kamble Jan 16 '23 at 04:34
  • Somebody If Possible answer to this question as well https://stackoverflow.com/questions/74995829/org-hibernate-mappingexception-could-not-locate-collectionpersister-for-role – Ankush Bandu Kamble Jan 16 '23 at 04:43

1 Answers1

2

The invocation of the actual method under test is missing in your testProductDelete_02 method. So the verification of deleteById also doesn't not happen.

If your aim is to test the deleteProduct method in the IProductServiceImpl class, your test case for the same should modified like below:

@Test
public void testProductDelete_02() {
   productService.deleteProduct(new Integer(1));
   verify(productRepository).deleteById(any());
}

Only after the actual invocation of the method in the service class is made, execution enters the method and control comes to the line repo.deleteById(prodId);. Now the method call is verified and this test method should pass

subasri_
  • 100
  • 6
  • 1
    I’d suggest you post this as another question so that anyone facing your previous question as issue might find this discussion meaningful and helpful – subasri_ Jan 15 '23 at 03:56