我想在JPA / Playframework中测试UniqueConstraint:
@Test
public void saveWithTitleNotUnique() { // not unique title
Software software1 = builder.template1().withTitle("title1").create();
Software software2 = builder.template1().withTitle("title1").create();
software1.save();
try {
software2.save();
fail("software.title is not unique");
} catch (Exception ex) {}
}
...
@Entity
public class Software extends Model {
@Column(unique = true)
public String title;
但是测试每次都失败,因为没有任何异常发生。
答案 0 :(得分:0)
你没有让软件实例有例外!
你必须做一个:Software software = new Software();
答案 1 :(得分:0)
我想通了:
我在构建Software实例时遇到了错误。因此,它起作用并抛出异常。
@Test
public void saveWithTitleNotUnique() { // not unique title
SoftwareTemplateBuilder builder1 = new SoftwareTemplateBuilder();
Software software1 = builder1.template1().withTitle("title1").create();
SoftwareTemplateBuilder builder2 = new SoftwareTemplateBuilder();
Software software2 = builder2.template1().withTitle("title1").create();
software1.save();
try {
software2.save();
fail("software.title is not unique");
} catch (PersistenceException ex) {}
}