I sklonowany this project i zmienił Hotel.java jak:testy Integracja nie podczas testowania @NotNull
@Entity
public class Hotel implements Serializable {
.
.
@javax.validation.constraints.NotNull
//@Column(nullable = false)
private String address;
.
.
}
składając wniosek po jak:
curl -H "Content-Type: application/json" -X POST -d '{}' http://localhost:8080/api/hotels
odpowiedź brzmi:
{
"timestamp": 1479213670718,
"status": 500,
"error": "Internal Server Error",
"exception": "javax.validation.ConstraintViolationException",
"message": "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [sample.data.rest.domain.Hotel] during persist time for groups [javax.validation.groups.Default, ]\nList of constraint violations:[\n\tConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=address, rootBeanClass=class sample.data.rest.domain.Hotel, messageTemplate='{javax.validation.constraints.NotNull.message}'}\n]",
"path": "/api/hotels"
}
Następnie napisałem test dla tego przypadku:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("scratch")
public class SampleDataRestApplicationTests {
.
.
@Test
public void createHotel_andExpectServerError() throws Exception {
this.mvc.perform(post("/api/hotels").content("{}"))
.andExpect(status().is5xxServerError());
}
}
Kiedy wykonać test, zgłasza ten błąd:
org.springframework.web.util.NestedServletException:
Request processing failed; nested exception is javax.validation.ConstraintViolationException:
Validation failed for classes [sample.data.rest.domain.Hotel] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{
interpolatedMessage='may not be null',
propertyPath=address, rootBeanClass=class sample.data.rest.domain.Hotel,
messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
i tak to nie, ale można oczekiwać, że zamiast przechodzić.
Wygląda na to, że domyślny wyjątekHandler, który obsługuje zwykle wyjątek ConstraintViolationException, nie jest dostępny w testach.
Dzięki.