Search code examples
javajpaintegration-testingspring-roo

Roo Integration Tests - FAILURE


Thought I would try out Spring Roo so I've had loads of new fun problems today. Hopefully you guys can help me with latest one. Roo has the ability to generate integration tests for your entities, but most of them fail for me. The most common failure is some form of constraint violation like null is being inserted into non-null fields. I realise that there is nothing on my entity that reflects these constraints in the database.

For instance the Property Entity has a many-to-one relationship with Term

//bi-directional many-to-one association to Term
    @ManyToOne
@JoinColumn(name="TERM_ID",)
private Term term;

Changing that to:

//bi-directional many-to-one association to Term
@ManyToOne(optional=false)
@JoinColumn(name="TERM_ID", nullable=false)
private Term term;

Still causes the test to fail so not sure if Roo is clever enough to create a Term entity automatically.

So my question is if I can modify these generated tests without Roo overwriting them on restart? Or should Roo be able to set non-null values?

There really is not much documentation about integration tests and what you can do with the generated classes anywhere. That I could find at least :)

Thanks


Solution

  • I stumbled upon this problem myself. Although this question is two years old I hope the question starter has also found a solution by now. Hopefully my answer helps other people.

    Apparently Roo uses the validation from JSR 303 instead of the JPA constraints in order to generate correct integration tests.

    JPA imposes constraints on the database, but not on the application logic. To let Roo generate correct integration tests you have to specify the same constraint as application logic as well. In order to have both, simply use annotations from JPA and JSR 303.

    ...
    import javax.validation.constraints.NotNull;
    ....
    
    @ManyToOne(optional=false)
    @JoinColumn(name="TERM_ID", nullable=false)
    @NotNull
    private Term term;