परीक्षण मैं this project क्लोन और तरह Hotel.java बदल दिया है:एकता परीक्षण में विफल रहता है जब @NotNull
@Entity
public class Hotel implements Serializable {
.
.
@javax.validation.constraints.NotNull
//@Column(nullable = false)
private String address;
.
.
}
जब बनाने एक पोस्ट अनुरोध की तरह:
curl -H "Content-Type: application/json" -X POST -d '{}' http://localhost:8080/api/hotels
प्रतिक्रिया है:
{
"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"
}
फिर मैंने इस मामले के लिए परीक्षण लिखा:
@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());
}
}
जब मैं परीक्षण चलाने के लिए, यह इस त्रुटि फेंकता है:
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}'}
]
और इसलिए यह विफल रहता है लेकिन मैं इसे बजाय पारित करने के लिए उम्मीद है।
यह डिफ़ॉल्ट अपवाद की तरह लगता है हैडलर जो कॉन्स्ट्रेनटिओलेशन अपवाद को संभालता है सामान्य रूप से परीक्षणों में उपलब्ध नहीं होता है।
धन्यवाद।