我有一个实体:@Entitypublic class MyEntity { @Id private String Id; @NotNull @Column(nullable = false) private Integer size; public void setSize(Integer size) { this.size = size; } public Integer getSize() { return this.size; } public void setId(String id) { this.id = id; } public String getId() { return this.id; }}一个存储库:@Repositorypublic class MyEntityDAO { @PersistenceContext private EntityManager em; public void create(MyEntity myEntity) { em.persist(myEntity); }}应该抛出异常的测试:@RunWith(SpringRunner.class)@Transactional@SpringBootTestpublic class MyEntityDAOTest { @Inject private MyEntityDAO myEntityDAO; @Test(expected = ConstraintViolationException.class) public void nullSizeNotAllowedTest() { MyEntity myEntity = new MyEntity(); myEntity.setSize(null); myEntity.setId("entity_id"); myEntityDAO.create(myEntity); }}但是测试失败。实体不会抛出所需的异常。注释适用于字符串,但不适用于整数。MyEntity 的自动生成表:FIELD TYPE NULL KEY DEFAULT ID VARCHAR(255) NO PRI NULLSIZE INTEGER(10) NO NULL
2 回答
蝴蝶不菲
TA贡献1810条经验 获得超4个赞
试试这个,在我的情况下按预期工作。
@Column(name = "size", nullable = false)
@NotNull(message= "size may not be empty.")
private Integer size;
添加回答
举报
0/150
提交
取消