Support target type in JsonPath assertions for MockMvc results
This commit picks up where SPR-14498 left off by adding support for an explicit target type when using JsonPath to perform an assertion against the response content using a Hamcrest Matcher. Specifically, there is a new overloaded value(Matcher<T>, Class<T>) method in JsonPathResultMatchers for use with Hamcrest matchers where the target type (i.e., Class<T>) can be specified. Issue: SPR-16587
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -18,6 +18,8 @@ package org.springframework.test.web.client.match;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.mock.http.client.MockClientHttpRequest;
|
||||
@@ -55,14 +57,19 @@ public class JsonPathRequestMatchersTests {
|
||||
}
|
||||
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void valueWithMismatch() throws Exception {
|
||||
new JsonPathRequestMatchers("$.str").value("bogus").match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void value() throws Exception {
|
||||
public void valueWithDirectMatch() throws Exception {
|
||||
new JsonPathRequestMatchers("$.str").value("foo").match(request);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void valueNoMatch() throws Exception {
|
||||
new JsonPathRequestMatchers("$.str").value("bogus").match(request);
|
||||
@Test // SPR-14498
|
||||
public void valueWithNumberConversion() throws Exception {
|
||||
new JsonPathRequestMatchers("$.num").value(5.0f).match(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -70,8 +77,13 @@ public class JsonPathRequestMatchersTests {
|
||||
new JsonPathRequestMatchers("$.str").value(equalTo("foo")).match(request);
|
||||
}
|
||||
|
||||
@Test // SPR-14498
|
||||
public void valueWithMatcherAndNumberConversion() throws Exception {
|
||||
new JsonPathRequestMatchers("$.num").value(Matchers.equalTo(5.0f), Float.class).match(request);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void valueWithMatcherNoMatch() throws Exception {
|
||||
public void valueWithMatcherAndMismatch() throws Exception {
|
||||
new JsonPathRequestMatchers("$.str").value(equalTo("bogus")).match(request);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2018 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.
|
||||
@@ -58,14 +58,19 @@ public class JsonPathResultMatchersTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void valueWithMismatch() throws Exception {
|
||||
new JsonPathResultMatchers("$.str").value("bogus").match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void value() throws Exception {
|
||||
public void valueWithDirectMatch() throws Exception {
|
||||
new JsonPathResultMatchers("$.str").value("foo").match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void valueNoMatch() throws Exception {
|
||||
new JsonPathResultMatchers("$.str").value("bogus").match(stubMvcResult);
|
||||
@Test // SPR-16587
|
||||
public void valueWithNumberConversion() throws Exception {
|
||||
new JsonPathResultMatchers("$.num").value(5.0f).match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -73,8 +78,13 @@ public class JsonPathResultMatchersTests {
|
||||
new JsonPathResultMatchers("$.str").value(Matchers.equalTo("foo")).match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test // SPR-16587
|
||||
public void valueWithMatcherAndNumberConversion() throws Exception {
|
||||
new JsonPathResultMatchers("$.num").value(Matchers.equalTo(5.0f), Float.class).match(stubMvcResult);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void valueWithMatcherNoMatch() throws Exception {
|
||||
public void valueWithMatcherAndMismatch() throws Exception {
|
||||
new JsonPathResultMatchers("$.str").value(Matchers.equalTo("bogus")).match(stubMvcResult);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user