単純なプロジェクトで、@NotNull 検証 (およびその他のカスタム検証) をテストするのが好きです。
したがって、これを実行するいくつかの単体テストを作成しました。@Test(expect=ValidationException.class
ここで github にアップロードした問題を再現するための最小限の mavinized の例:
- https://github.com/d0x/questions/tree/master/givenIdValidation
実行
mvn clean test
@Id
が生成された値であればうまく機能することを認識しました。しかし@Id
、システムによって が与えられた場合、検証は無視されます。
このクラスは、問題を再現するための最小限のセットアップを示します。
2 つのエンティティ (1 つは生成された値を持ち、もう 1 つは値を持たない:
@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;
}
単体テスト:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:/applicationContext.xml")
@Transactional
@ActiveProfiles("embedded")
public class MyEntityTest
{
@Autowired GeneratedIdService generatedIdService;
@Autowired GivenIdService givenIdService;
// This test will pass
@Test(expected = ValidationException.class)
public void shouldNotAllowNullValues1()
{
this.generatedIdService.save(new GeneratedId());
}
// This test will fail
@Test(expected = ValidationException.class)
public void shouldNotAllowNullValues2()
{
this.givenIdService.save(new GivenId(1L, null));
}
}
これはボイラープレートのサービスとリポジトリです
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);
}
}
現在、Spring 3.1.4、Spring-Data 1.3.4、Hibernate 4.1.10、および Hibernate-Validator 4.2.0 を使用しています。
検証がスキップされた方法について何か提案はありますか?
編集1:
両方のエンティティでロンボクなしで試しましたが、それでもエラーが発生します。