Support target type in JsonPath assertions
This change adds support for a target type in JsonPath assertions in
Spring MVC Test.
The existing assertValue(String expression, Object expectedValue)
transparently falls back on using an alternative JsonPath API that
allows specifying the target type to coerce to.
There is also a new overloaded method
assertValue(String expression, Matcher<T> matcher, Class<T> targetType)
for use with Hamcrest matchers where the target type can be specified.
Issue: SPR-14498
(cherry picked from commit 7fdb892)
This commit is contained in:
committed by
Juergen Hoeller
parent
798d8668a4
commit
d09b0fe83a
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2004-2015 the original author or authors.
|
||||
* Copyright 2004-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,7 +20,7 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.core.Is.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link JsonPathExpectationsHelper}.
|
||||
@@ -222,11 +222,14 @@ public class JsonPathExpectationsHelperTests {
|
||||
new JsonPathExpectationsHelper("$.num").assertValue(CONTENT, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void assertValueWithDifferentExpectedType() throws Exception {
|
||||
exception.expect(AssertionError.class);
|
||||
exception.expectMessage(equalTo("At JSON path \"$.num\", type of value expected:<java.lang.String> but was:<java.lang.Integer>"));
|
||||
new JsonPathExpectationsHelper("$.num").assertValue(CONTENT, "5");
|
||||
@Test // SPR-14498
|
||||
public void assertValueWithNumberConversion() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.num").assertValue(CONTENT, 5.0);
|
||||
}
|
||||
|
||||
@Test // SPR-14498
|
||||
public void assertValueWithNumberConversionAndMatcher() throws Exception {
|
||||
new JsonPathExpectationsHelper("$.num").assertValue(CONTENT, is(5.0), Double.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user