异常发生在网站上,mockito需要但未被调用,实际上与此mock没有交互

wkyowqbh  于 2023-01-17  发布在  其他
关注(0)|答案(1)|浏览(151)
**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;
    }

异常发生在网站上。mockito需要但未被调用,实际上提取类型[class com. junittesting. dto. ProductDto]的响应时与此mock没有交互OccuringError从JPA存储库调用此方法时。我得到mockito wantedm尝试编写单元测试用例但未被调用。实际上在验证www.example.com错误时与此mock没有交互call.giving error

qojgxg4l

qojgxg4l1#

testProductDelete_02方法中缺少对实际测试方法的调用,因此deleteById的验证也不会发生。
如果你的目标是测试IProductServiceImpl类中的deleteProduct方法,那么你的测试用例应该修改如下:

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

只有在实际调用服务类中的方法之后,执行才会进入该方法,控制才会转到repo.deleteById(prodId);行,现在方法调用已经过验证,该测试方法应该通过

相关问题