**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
1条答案
按热度按时间qojgxg4l1#
在
testProductDelete_02
方法中缺少对实际测试方法的调用,因此deleteById
的验证也不会发生。如果你的目标是测试
IProductServiceImpl
类中的deleteProduct
方法,那么你的测试用例应该修改如下:只有在实际调用服务类中的方法之后,执行才会进入该方法,控制才会转到
repo.deleteById(prodId);
行,现在方法调用已经过验证,该测试方法应该通过