Error with validator (for type: java.util.Date no validator found).

My friends,

I have a class with ...

@NotBlank(message = "timesheet.cadastro.horainicio.obrigatorio") @Temporal(TemporalType.TIME) @Column(name = "INICIO", nullable = false) private Date horaInicio; 

And, in my test (groovy), I put null in "horaInicio":

 def "Salvar um timesheet sem hora inicio"() { given:"um timesheet sem data" def usuario = sessionFactory.getCurrentSession().get(Usuario.class,1L) as Usuario def dias = sessionFactory.getCurrentSession().get(Dias.class,1L) as Dias def timeSheet = criarTimeSheet(usuario,dias) as TimeSheet timeSheet.horaInicio = null when:"buscar na base" def timeSheetPersistido = timeSheetService.salvar(timeSheet) then:"retorna uma lista de timesheet" def erro = thrown(ConstraintViolationException) "timesheet.cadastro.horainicio.obrigatorio".equals(erro.getConstraintViolations().asList().get(0).getMessage()) } 

but I have an error:

 Expected exception javax.validation.ConstraintViolationException, but got javax.validation.UnexpectedTypeException at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:79) at br.com.infowhere.service.TimeSheetServiceIt.Salvar um timesheet sem hora inicio(TimeSheetServiceIt.groovy:80) Caused by: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.util.Date. 

Can anybody help me?

thanks

+7
source share
3 answers

As the documentation says, @NotBlank for type String. There is no concept of java.util.Date to be empty. It may be null or invalid.

Confirm that the annotated string is not null or empty. the difference in NotEmpty is that trailing spaces are ignored.

If the goal is to check the value is not null, @NotNull . According to the documentation:

Annotated element must not be null. Accepts any type.

+26
source
 @Temporal(DATE) @Column(name = "creation_timestamp") private Date creationTimestamp; 

or

 @Temporal(TIMESTAMP) @Column(name = "creation_timestamp") private Date creationTimestamp; 

if the type of db is 'datetime'.

Simple and it works. no additional @annotations required.

0
source

juste delete @Temporal(TemporalType.TIME) annotation; -)

-2
source

All Articles