2016-11-15 10 views
5

परीक्षण मैं 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}'} 
] 

और इसलिए यह विफल रहता है लेकिन मैं इसे बजाय पारित करने के लिए उम्मीद है।

यह डिफ़ॉल्ट अपवाद की तरह लगता है हैडलर जो कॉन्स्ट्रेनटिओलेशन अपवाद को संभालता है सामान्य रूप से परीक्षणों में उपलब्ध नहीं होता है।

धन्यवाद।

उत्तर

0

मैं आपके परिणाम को पुन: पेश नहीं कर सकता, लेकिन शायद कुछ चीजों को स्पष्ट करके अपने मुद्दे के करीब होना संभव है।

अगर मैं अपने मशीन पर (हेडर प्राप्त करने के लिए एक अतिरिक्त -v ध्वज के साथ) अपने कर्ल आदेश पर अमल, मैं एक ग्राहक त्रुटि मिलती है, सटीक 409 Conflict किया जाना है।

curl -v -H "Content-Type: application/json" -X POST -d '{}' http://localhost:8080/api/hotels 
Note: Unnecessary use of -X or --request, POST is already inferred. 
* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 8080 (#0) 
> POST /api/hotels HTTP/1.1 
> Host: localhost:8080 
> User-Agent: curl/7.47.0 
> Accept: */* 
> Content-Type: application/json 
> Content-Length: 2 
> 
* upload completely sent off: 2 out of 2 bytes 
< HTTP/1.1 409 Conflict 
< Date: Sun, 22 Jan 2017 12:29:45 GMT 
< Content-Type: application/hal+json;charset=UTF-8 
< Transfer-Encoding: chunked 
< 
* Connection #0 to host localhost left intact 
{"cause":{"cause":{"cause":null,"message":"NULL not allowed for column \"ADDRESS\"; SQL statement:\ninsert into hotel (id, address, city_id, name, zip) values (null, ?, ?, ?, ?) [23502-193]"},"message":"could not execute statement"},"message":"could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"} 

ही (STS) मेरी आईडीई के भीतर अपने परीक्षण को क्रियान्वित होता है:

java.lang.AssertionError: Range for response status value 409 expected:<SERVER_ERROR> but was:<CLIENT_ERROR> 
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:54) 
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:81) 
    at org.springframework.test.web.servlet.result.StatusResultMatchers$7.match(StatusResultMatchers.java:132) 
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171) 
    ... 
पहली बार में

तो वहाँ परीक्षण और चल रहे एप्लिकेशन के बीच कोई अंतर नहीं है, DataIntegrityViolationException/ConstraintViolationException को दोनों ही मामलों में मैप किया गया है 40 9.

यदि यह सही है तो एक और सवाल है, लेकिन शायद गलत/अमान्य डेटा भेजना वास्तव में ग्राहक की गलती है।

आप अभी भी है कि बदलना चाहते हैं, तो आप इस तरह एक तरह से हो सकता है अपनी खुद की ExceptionHandler जोड़ने के लिए:

import org.hibernate.exception.ConstraintViolationException; 
import org.springframework.http.HttpHeaders; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.bind.annotation.ExceptionHandler; 
import org.springframework.web.bind.annotation.RestControllerAdvice; 
import org.springframework.web.context.request.WebRequest; 
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 

@RestControllerAdvice 
public class ConstraintViolationExceptionHandler extends ResponseEntityExceptionHandler { 

    @ExceptionHandler({ ConstraintViolationException.class }) 
    public ResponseEntity<Object> handleConstraintViolationException(Exception ex, WebRequest request) { 
     return handleExceptionInternal(ex, "I think it's the servers fault!", new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); 
    } 

} 

हो सकता है कि आपकी समस्या का समाधान कर सकते हैं resp,। आपको एक संकेत दें या क्या कोई और सवाल है जो मुझे नहीं मिला?

संबंधित मुद्दे