In a simple project, I like to test @NotNull validation (and some other custom ones).
So I wrote a few unittests that do this: @Test(expect=ValidationException.class
The minimum mavinized example to reproduce the problem I uploaded to github here:
I registered that it works well if @Id is a generated value. But if @Id is set by the system, the check is ignored.
These classes will show the minimum setting to reproduce the problem:
Two objects (one with a generated value, one with an address:
@Data @NoArgsConstructor @AllArgsConstructor @Entity public class GeneratedId { @Id @GeneratedValue private Long id; @NotNull private String content; } @Data @NoArgsConstructor @AllArgsConstructor @Entity public class GivenId { @Id private Long id; @NotNull private String content; }
unit test:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath*:/applicationContext.xml") @Transactional @ActiveProfiles("embedded") public class MyEntityTest { @Autowired GeneratedIdService generatedIdService; @Autowired GivenIdService givenIdService;
This is a service and storage
public interface GeneratedIdRepository extends JpaRepository<GeneratedId, Long> { } public interface GivenIdRepository extends JpaRepository<GivenId, Long> { } @Service public class GeneratedIdService { @Autowired GeneratedIdRepository repository; public GeneratedId save(final GeneratedId entity) { return this.repository.save(entity); } } @Service public class GivenIdService { @Autowired GivenIdRepository repository; public GivenId save(final GivenId entity) { return this.repository.save(entity); } }
I am currently using Spring 3.1.4, Spring-Data 1.3.4, Hibernate 4.1.10 and Hibernate-Validator 4.2.0.
Any suggestion on how the check was skipped?
Change 1:
I tried this without lombok on both entities, still an error occurs.