White box test requires that each line of code be covered at least once.
@ApiModelProperty("Disciple Category")@ManyToOne// Set the subject category field to be empty @JoinColumn(nullable = false)@JsonView({NoneJsonView.class, MeasurementUnitCategoryJsonView.getAllByDisciplineId.class})private Discipline discipline;Taking the previous example of setting the subject not empty as a case, we need to test two situations: exceptions when they are empty and save them normally when they are not empty.
@Testpublic void saveTest() { logger.debug("New Unit of Measurement Category"); MeasurementUnitCategory measurementUnitCategory = new MeasurementUnitCategory(); logger.debug("Test Save"); measurementUnitCategoryService.save(measurementUnitCategory);} Here we called the save method, but IDE did not prompt us to catch the exception, but it does not mean that the save method does not throw an exception. It can throw a non-checked RuntimeException or its derived exception.
To test this exception, we first run this line of code to see what exception appears.
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: org.h2.jdbc.JdbcSQLException: NULL not allowed for column "DISCIPLINE_ID"; SQL statement:
insert into measurement_unit_category (id, discipline_id, is_asc) values (null, ?, ?) [23502-194]
We see three exceptions, first, JdbcSQLException when inserting this record, then the exception causes ConstraintViolationException , and the new exception causes DataIntegrityViolationException .
This Caused by is actually an exception encapsulation. For example, the underlying layer may throw exceptions, but we generally handle exceptions at a relatively high level.
Take this as an example. DataIntegrityViolationException data violates exceptions, and many types of exceptions may occur, so the processing methods of this exception are the same or similar.
When the underlying layer throws a JdbcSQLException , then calls its method to catch the exception, and builds a new exception ConstraintViolationException with the exception (restriction violation exception), then throws it to the upper layer, and then catches it to the upper layer, builds a new exception DataIntegrityViolationException and throws it to us. We did not handle it, and then the console reports an error.
The advantage of keeping encapsulating and throwing upwards is that I can use an exception to handle a similar situation, and then when dealing with this exception, I can trace the source and keep accurate to what caused it. If this package is not available, we need to directly go to catch underlying exceptions to accurately locate the error.
OK, the exception we need to catch here is the DataIntegrityViolationException exception thrown to us by the application.
@Testpublic void saveTest() { logger.debug("Basic test data preparation"); MeasurementUnitCategory measurementUnitCategory = new MeasurementUnitCategory(); Boolean catchException = false; logger.debug("Test save, expect to throw exception"); try { measurementUnitCategoryService.save(measurementUnitCategory); } catch (DataIntegrityViolationException e) { catchException = true; } logger.debug("Assert that the capture exception is true"); assertThat(catchException).isTrue();}Run the test and pass.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.