我有一个实体:
@Entity
public 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; }
}
一个存储库:
@Repository
public class MyEntityDAO {
@PersistenceContext
private EntityManager em;
public void create(MyEntity myEntity) {
em.persist(myEntity);
}
}
应该抛出异常的测试:
@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest
public 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 NULL
SIZE INTEGER(10) NO NULL
蝴蝶不菲
相关分类