Introduce additional JsonPath matchers in Spring MVC Test

This commit introduces the following methods in JsonPathResultMatchers
in the Spring MVC Test framework.

- isString()
- isBoolean()
- isNumber()
- isMap()

In addition, this commit overhauls the Javadoc in
JsonPathResultMatchers and JsonPathExpectationsHelper.

Issue: SPR-13320
This commit is contained in:
Craig Andrews
2015-08-06 00:47:33 -04:00
committed by Sam Brannen
parent 48b965ad33
commit fffdd1e9e9
5 changed files with 293 additions and 104 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2004-2013 the original author or authors.
* Copyright 2004-2015 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.
@@ -16,28 +16,30 @@
package org.springframework.test.util;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
/**
* Test fixture for {@link JsonPathExpectationsHelper}.
* Unit tests for {@link JsonPathExpectationsHelper}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 3.2
*/
public class JsonPathExpectationsHelperTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void test() throws Exception {
try {
new JsonPathExpectationsHelper("$.nr").assertValue("{ \"nr\" : 5 }", "5");
fail("Expected exception");
}
catch (AssertionError ex) {
assertEquals("For JSON path $.nr type of value expected:<class java.lang.String> but was:<class java.lang.Integer>",
ex.getMessage());
}
public void assertValueWithDifferentExpectedType() throws Exception {
exception.expect(AssertionError.class);
exception.expectMessage(equalTo("For JSON path \"$.nr\", type of value expected:<java.lang.String> but was:<java.lang.Integer>"));
new JsonPathExpectationsHelper("$.nr").assertValue("{ \"nr\" : 5 }", "5");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 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.
@@ -17,6 +17,7 @@
package org.springframework.test.web.servlet.result;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -26,6 +27,7 @@ import org.springframework.test.web.servlet.StubMvcResult;
* Tests for {@link JsonPathResultMatchers}.
*
* @author Rossen Stoyanchev
* @author Craig Andrews
*/
public class JsonPathResultMatchersTests {
@@ -34,7 +36,7 @@ public class JsonPathResultMatchersTests {
new JsonPathResultMatchers("$.foo").value("bar").match(getStubMvcResult());
}
@Test(expected=AssertionError.class)
@Test(expected = AssertionError.class)
public void valueNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").value("bogus").match(getStubMvcResult());
}
@@ -44,7 +46,7 @@ public class JsonPathResultMatchersTests {
new JsonPathResultMatchers("$.foo").value(Matchers.equalTo("bar")).match(getStubMvcResult());
}
@Test(expected=AssertionError.class)
@Test(expected = AssertionError.class)
public void valueMatcherNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").value(Matchers.equalTo("bogus")).match(getStubMvcResult());
}
@@ -54,7 +56,7 @@ public class JsonPathResultMatchersTests {
new JsonPathResultMatchers("$.foo").exists().match(getStubMvcResult());
}
@Test(expected=AssertionError.class)
@Test(expected = AssertionError.class)
public void existsNoMatch() throws Exception {
new JsonPathResultMatchers("$.bogus").exists().match(getStubMvcResult());
}
@@ -64,7 +66,7 @@ public class JsonPathResultMatchersTests {
new JsonPathResultMatchers("$.bogus").doesNotExist().match(getStubMvcResult());
}
@Test(expected=AssertionError.class)
@Test(expected = AssertionError.class)
public void doesNotExistNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").doesNotExist().match(getStubMvcResult());
}
@@ -74,13 +76,54 @@ public class JsonPathResultMatchersTests {
new JsonPathResultMatchers("$.qux").isArray().match(getStubMvcResult());
}
@Test(expected=AssertionError.class)
@Test(expected = AssertionError.class)
public void isArrayNoMatch() throws Exception {
new JsonPathResultMatchers("$.bar").isArray().match(getStubMvcResult());
}
@Test
public void isBooleanMatch() throws Exception {
new JsonPathResultMatchers("$.icanhaz").isBoolean().match(getStubMvcResult());
}
@Test(expected = AssertionError.class)
public void isBooleanNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").isBoolean().match(getStubMvcResult());
}
@Test
public void isNumberMatch() throws Exception {
new JsonPathResultMatchers("$.howmanies").isNumber().match(getStubMvcResult());
}
@Test(expected = AssertionError.class)
public void isNumberNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").isNumber().match(getStubMvcResult());
}
@Test
public void isMapMatch() throws Exception {
new JsonPathResultMatchers("$.cheeseburger").isMap().match(getStubMvcResult());
}
@Test(expected = AssertionError.class)
public void isMapNoMatch() throws Exception {
new JsonPathResultMatchers("$.foo").isMap().match(getStubMvcResult());
}
@Test
public void isStringMatch() throws Exception {
new JsonPathResultMatchers("$.foo").isString().match(getStubMvcResult());
}
@Test(expected = AssertionError.class)
public void isStringNoMatch() throws Exception {
new JsonPathResultMatchers("$.qux").isString().match(getStubMvcResult());
}
private static final String RESPONSE_CONTENT = "{\"foo\":\"bar\", \"qux\":[\"baz1\",\"baz2\"], \"icanhaz\":true, \"howmanies\": 5, \"cheeseburger\": {\"pickles\": true} }";
private static final String RESPONSE_CONTENT = "{\"foo\":\"bar\", \"qux\":[\"baz1\",\"baz2\"]}";
private StubMvcResult getStubMvcResult() throws Exception {
MockHttpServletResponse response = new MockHttpServletResponse();