Hibernate 使用@NotBlank注释提交事务时测试失败,出现错误
以下是包含@NotBlank注释的实体:Hibernate 使用@NotBlank注释提交事务时测试失败,出现错误,hibernate,spring-boot,validation,exception,Hibernate,Spring Boot,Validation,Exception,以下是包含@NotBlank注释的实体: import javax.validation.constraints.NotBlank; //... @Entity(name = "funds") @SQLDelete(sql = "UPDATE funds SET deleted_at = now() WHERE id = ?") @Where(clause = "deleted_at is null") public class Fund implements Serializable {
import javax.validation.constraints.NotBlank;
//...
@Entity(name = "funds")
@SQLDelete(sql = "UPDATE funds SET deleted_at = now() WHERE id = ?")
@Where(clause = "deleted_at is null")
public class Fund implements Serializable {
@Column(nullable = false)
@NotBlank(message = "Name is mandatory")
private String name;
...
这是我的休息控制器:
@RestController
@RequestMapping("/funds")
public class FundController
{
@Autowired
private FundRepository fundRepository;
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public @ResponseBody Fund create(
@RequestParam("name") String name,
@RequestParam(value = "amount", required = false, defaultValue = "0") Double amount,
@RequestParam("currency") Integer currencyId,
Principal principal) {
Fund fund = new Fund(name, amount);
//...
return fundRepository.save(fund);
}
//...
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public String handleValidationExceptions(ConstraintViolationException e) {
return e.getMessage();
}
下面是对字段验证器的测试:
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class)
public class FundsControllerTest extends ControllerTest {
@Test
public void testCreateFund_whenFieldsInValid_thenBadRequest() throws Exception {
String accessToken = getAccessToken(USER_USERNAME, USER_PASSWORD);
mockMvc.perform(
post("/funds/create")
.header("Authorization", "Bearer " + accessToken)
.param("name", "") // empty
.param("amount", "1000")
.param("currency", currency.getId().toString())
).andExpect(status().isBadRequest());
}
但是,我看到了以下错误:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 12.611 s <<< FAILURE! - in biz.martyn.budget.FundsControllerTest
[ERROR] testCreateFund_whenFieldsInValid_thenBadRequest(biz.martyn.budget.FundsControllerTest) Time elapsed: 1.533 s <<< ERROR!
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
at biz.martyn.budget.FundsControllerTest.testCreateFund_whenFieldsInValid_thenBadRequest(FundsControllerTest.java:109)
Caused by: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
at biz.martyn.budget.FundsControllerTest.testCreateFund_whenFieldsInValid_thenBadRequest(FundsControllerTest.java:109)
Caused by: javax.persistence.RollbackException: Error while committing the transaction
at biz.martyn.budget.FundsControllerTest.testCreateFund_whenFieldsInValid_thenBadRequest(FundsControllerTest.java:109)
Caused by: javax.validation.ConstraintViolationException:
Validation failed for classes [biz.martyn.budget.fund.Fund] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='must not be blank', propertyPath=name, rootBeanClass=class biz.martyn.budget.fund.Fund, messageTemplate='{javax.validation.constraints.NotBlank.message}'}
]
at biz.martyn.budget.FundsControllerTest.testCreateFund_whenFieldsInValid_thenBadRequest(FundsControllerTest.java:109)
[ERROR]测试运行:1,失败:0,错误:1,跳过:0,经过的时间:12.611s@NotBlank
表示名称
不能包含空字符串。但是您输入了一个空字符串,因此会得到一个异常。我不知道你为什么会期望其他任何东西。我不希望得到一个未处理的异常。我希望得到4xx的回复。当一个字段无效时,为什么它会试图插入数据库?好的,这是关于异常转换和处理的更多信息。这些可能会有帮助:为什么它在不应该验证的情况下尝试更新数据库?@NotBlank被忽略了,这就是我想要理解的。