0

我想在 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;

但是每次都测试失败,因为没有任何异常发生。

4

2 回答 2

0

您还没有将软件实例设为例外!你必须做一个:Software software = new Software();

于 2012-12-27T09:47:31.380 回答
0

我想通了:

我在构建软件实例时犯了一个错误。因此,它可以工作并引发异常。

@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) {}
}
于 2012-12-27T21:59:20.690 回答