我正在使用Spock和RestAssured对rest服务进行一些集成测试。 我在使用hamcrest的closeTo
匹配器检查双精度值时遇到了问题:
class PlaceITSpec extends Specification {
def "Create a Place"() {
given:
def place = ['name' : 'Depot A',
'location': [
'latitude': 40d,
'longitude': -3d
]]
def request = given().accept(ContentType.JSON).contentType(ContentType.JSON).body(place)
when: "POST /places"
def response = request.with().post("http://localhost:8080/places")
then: "I get a the created place resource and 201 status code"
response.then().log().all()
.statusCode(201)
.body("name", equalTo(place['name']))
.body("location.longitude", is(closeTo(place['location']['longitude'], 0.000001d)))
.body("location.latitude", is(closeTo(place['location']['latitude'], 0.000001d)))
.body("_links.self", notNullValue())
.body("_links.place", notNullValue())
}
}
测试失败,出现以下错误信息:
java.lang.AssertionError: 1 expectation failed.
JSON path location.longitude doesn't match.
Expected: is a numeric value within <1.0E-6> of <-3.0>
Actual: -3.0
一切似乎都正常。为了排除明显的错误,我使用了一个1的错误值。然而测试失败并显示:
java.lang.AssertionError: 1 expectation failed.
JSON path location.longitude doesn't match.
Expected: is a numeric value within <1.0> of <-3.0>
Actual: -3.0
我猜我做错了什么。有人遇到过同样的问题吗?
-3f
进行比较,而不是使用双精度数-3d
。 - Daniel Cerecedo