我是靠谱客的博主 贪玩白羊,最近开发中收集的这篇文章主要介绍java测试不成功_java – 测试@NotNull时集成测试失败,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我无法重现您的结果,但也许通过澄清一些事情可以更接近您的问题.

如果我在我的机器上执行你的curl命令(带有一个额外的-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"}

在我的IDE(STS)中执行测试也是如此:

java.lang.AssertionError: Range for response status value 409 expected: but was:

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在两种情况下都映射到409.

如果这是正确的另一个问题,但可能发送错误/无效数据实际上是客户端错误.

如果您仍想更改它,则必须以这样的方式添加自己的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 handleConstraintViolationException(Exception ex, WebRequest request) {

return handleExceptionInternal(ex, "I think it's the servers fault!", new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);

}

}

也许这可以解决你的问题,resp.给你一个提示.或者还有另一个我没有得到的问题?

最后

以上就是贪玩白羊为你收集整理的java测试不成功_java – 测试@NotNull时集成测试失败的全部内容,希望文章能够帮你解决java测试不成功_java – 测试@NotNull时集成测试失败所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部