Hamcrest 匹配器 closeTo 无法按预期工作
Hamcrest matcher closeTo not working as expected
我正在使用 Spock 和 RestAssured 对休息服务执行一些集成测试。我在使用 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
我想我做错了什么。有人遇到同样的问题吗?
终于通过RestAssured doc
找到了正确答案
Floating point numbers must be compared with a Java "float" primitive
我将代码更新为:
class PlaceITSpec extends Specification {
def "Create a Place"() {
given:
def place = ['name' : 'Depot A',
'location': [
'latitude': 40f,
'longitude': -3f
]]
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", equalTo(place['location']['longitude']))
.body("location.latitude", equalTo(place['location']['latitude']))
.body("_links.self", notNullValue())
.body("_links.place", notNullValue())
}
}
检查纬度和经度值现在是否具有 f
后缀以指示 float
原语不是 double
原语。并且还将 is(closeTo(data,error))
更改为 equalTo(data)
我正在使用 Spock 和 RestAssured 对休息服务执行一些集成测试。我在使用 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
我想我做错了什么。有人遇到同样的问题吗?
终于通过RestAssured doc
找到了正确答案Floating point numbers must be compared with a Java "float" primitive
我将代码更新为:
class PlaceITSpec extends Specification {
def "Create a Place"() {
given:
def place = ['name' : 'Depot A',
'location': [
'latitude': 40f,
'longitude': -3f
]]
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", equalTo(place['location']['longitude']))
.body("location.latitude", equalTo(place['location']['latitude']))
.body("_links.self", notNullValue())
.body("_links.place", notNullValue())
}
}
检查纬度和经度值现在是否具有 f
后缀以指示 float
原语不是 double
原语。并且还将 is(closeTo(data,error))
更改为 equalTo(data)