From f7e75a5b82e580329782207c1832585186b9efaa Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 15 Mar 2018 10:30:42 -0400 Subject: [PATCH] Polish MockRestServiceServer client tests --- ...ontentRequestMatchersIntegrationTests.java | 20 +++++++++------- ...HeaderRequestMatchersIntegrationTests.java | 10 +++++--- ...onPathRequestMatchersIntegrationTests.java | 24 +++++++++++++++---- ...ontentRequestMatchersIntegrationTests.java | 10 +++++--- .../XpathRequestMatchersIntegrationTests.java | 22 ++++++++--------- 5 files changed, 56 insertions(+), 30 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java index e8072d4772..228debce0f 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/ContentRequestMatchersIntegrationTests.java @@ -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. @@ -17,6 +17,7 @@ package org.springframework.test.web.client.samples.matchers; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -66,15 +67,14 @@ public class ContentRequestMatchersIntegrationTests { @Test public void contentType() throws Exception { this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess()); - this.restTemplate.put(new URI("/foo"), new Person()); - this.mockServer.verify(); + executeAndVerify(new Person()); } @Test public void contentTypeNoMatch() throws Exception { this.mockServer.expect(content().contentType("application/json;charset=UTF-8")).andRespond(withSuccess()); try { - this.restTemplate.put(new URI("/foo"), "foo"); + executeAndVerify("foo"); } catch (AssertionError error) { String message = error.getMessage(); @@ -85,21 +85,23 @@ public class ContentRequestMatchersIntegrationTests { @Test public void contentAsString() throws Exception { this.mockServer.expect(content().string("foo")).andRespond(withSuccess()); - this.restTemplate.put(new URI("/foo"), "foo"); - this.mockServer.verify(); + executeAndVerify("foo"); } @Test public void contentStringStartsWith() throws Exception { this.mockServer.expect(content().string(startsWith("foo"))).andRespond(withSuccess()); - this.restTemplate.put(new URI("/foo"), "foo123"); - this.mockServer.verify(); + executeAndVerify("foo123"); } @Test public void contentAsBytes() throws Exception { this.mockServer.expect(content().bytes("foo".getBytes())).andRespond(withSuccess()); - this.restTemplate.put(new URI("/foo"), "foo"); + executeAndVerify("foo"); + } + + private void executeAndVerify(Object body) throws URISyntaxException { + this.restTemplate.put(new URI("/foo"), body); this.mockServer.verify(); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/HeaderRequestMatchersIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/HeaderRequestMatchersIntegrationTests.java index 6fd57afb51..1e1e450e2d 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/HeaderRequestMatchersIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/HeaderRequestMatchersIntegrationTests.java @@ -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. @@ -17,6 +17,7 @@ package org.springframework.test.web.client.samples.matchers; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; @@ -69,8 +70,7 @@ public class HeaderRequestMatchersIntegrationTests { .andExpect(header("Accept", "application/json, application/*+json")) .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON)); - this.restTemplate.getForObject(new URI("/person/1"), Person.class); - this.mockServer.verify(); + executeAndVerify(); } @Test @@ -79,6 +79,10 @@ public class HeaderRequestMatchersIntegrationTests { .andExpect(header("Accept", containsString("json"))) .andRespond(withSuccess(RESPONSE_BODY, MediaType.APPLICATION_JSON)); + executeAndVerify(); + } + + private void executeAndVerify() throws URISyntaxException { this.restTemplate.getForObject(new URI("/person/1"), Person.class); this.mockServer.verify(); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.java index 500536ef0c..5ac1854776 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/JsonPathRequestMatchersIntegrationTests.java @@ -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. @@ -21,7 +21,6 @@ import java.net.URISyntaxException; import java.util.Arrays; import java.util.Collections; -import org.junit.After; import org.junit.Test; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; @@ -73,6 +72,8 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(jsonPath("$.composers[2]").exists()) .andExpect(jsonPath("$.composers[3]").exists()) .andRespond(withSuccess()); + + executeAndVerify(); } @Test @@ -83,6 +84,8 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(jsonPath("$.composers[?(@.name == 'Robert Schuuuuuuman')]").doesNotExist()) .andExpect(jsonPath("$.composers[4]").doesNotExist()) .andRespond(withSuccess()); + + executeAndVerify(); } @Test @@ -92,6 +95,8 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(jsonPath("$.composers[0].name").value("Johann Sebastian Bach")) .andExpect(jsonPath("$.performers[1].name").value("Yehudi Menuhin")) .andRespond(withSuccess()); + + executeAndVerify(); } @Test @@ -106,6 +111,8 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(jsonPath("$.composers[1].name", isIn(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))) .andExpect(jsonPath("$.composers[:3].name", hasItem("Johannes Brahms"))) .andRespond(withSuccess()); + + executeAndVerify(); } @Test @@ -120,6 +127,8 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(jsonPath(performerName, 1).value(containsString("di Me"))) .andExpect(jsonPath(composerName, 1).value(isIn(Arrays.asList("Johann Sebastian Bach", "Johannes Brahms")))) .andRespond(withSuccess()); + + executeAndVerify(); } @Test @@ -128,6 +137,8 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers").isArray()) .andRespond(withSuccess()); + + executeAndVerify(); } @Test @@ -136,6 +147,8 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0].name").isString()) .andRespond(withSuccess()); + + executeAndVerify(); } @Test @@ -144,6 +157,8 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0].someDouble").isNumber()) .andRespond(withSuccess()); + + executeAndVerify(); } @Test @@ -152,10 +167,11 @@ public class JsonPathRequestMatchersIntegrationTests { .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.composers[0].someBoolean").isBoolean()) .andRespond(withSuccess()); + + executeAndVerify(); } - @After - public void performRequestAndVerify() throws URISyntaxException { + private void executeAndVerify() throws URISyntaxException { this.restTemplate.put(new URI("/composers"), people); this.mockServer.verify(); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/XmlContentRequestMatchersIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/XmlContentRequestMatchersIntegrationTests.java index 5fe8267264..2c4e7dbb8e 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/XmlContentRequestMatchersIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/XmlContentRequestMatchersIntegrationTests.java @@ -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. @@ -17,6 +17,7 @@ package org.springframework.test.web.client.samples.matchers; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -91,8 +92,7 @@ public class XmlContentRequestMatchersIntegrationTests { .andExpect(content().xml(PEOPLE_XML)) .andRespond(withSuccess()); - this.restTemplate.put(new URI("/composers"), this.people); - this.mockServer.verify(); + executeAndVerify(); } @Test @@ -102,6 +102,10 @@ public class XmlContentRequestMatchersIntegrationTests { .andExpect(content().node(hasXPath("/people/composers/composer[1]"))) .andRespond(withSuccess()); + executeAndVerify(); + } + + private void executeAndVerify() throws URISyntaxException { this.restTemplate.put(new URI("/composers"), this.people); this.mockServer.verify(); } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/XpathRequestMatchersIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/XpathRequestMatchersIntegrationTests.java index 0d97942d88..420aaf8067 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/XpathRequestMatchersIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/samples/matchers/XpathRequestMatchersIntegrationTests.java @@ -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. @@ -17,6 +17,7 @@ package org.springframework.test.web.client.samples.matchers; import java.net.URI; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -100,8 +101,7 @@ public class XpathRequestMatchersIntegrationTests { .andExpect(xpath(performer, NS, 2).exists()) .andRespond(withSuccess()); - this.restTemplate.put(new URI("/composers"), this.people); - this.mockServer.verify(); + executeAndVerify(); } @Test @@ -117,8 +117,7 @@ public class XpathRequestMatchersIntegrationTests { .andExpect(xpath(performer, NS, 3).doesNotExist()) .andRespond(withSuccess()); - this.restTemplate.put(new URI("/composers"), this.people); - this.mockServer.verify(); + executeAndVerify(); } @Test @@ -139,8 +138,7 @@ public class XpathRequestMatchersIntegrationTests { .andExpect(xpath(composerName, NS, 1).string(notNullValue())) // Hamcrest.. .andRespond(withSuccess()); - this.restTemplate.put(new URI("/composers"), this.people); - this.mockServer.verify(); + executeAndVerify(); } @Test @@ -157,8 +155,7 @@ public class XpathRequestMatchersIntegrationTests { .andExpect(xpath(composerDouble, NS, 3).number(closeTo(1.6, .01))) // Hamcrest.. .andRespond(withSuccess()); - this.restTemplate.put(new URI("/composers"), this.people); - this.mockServer.verify(); + executeAndVerify(); } @Test @@ -172,8 +169,7 @@ public class XpathRequestMatchersIntegrationTests { .andExpect(xpath(performerBooleanValue, NS, 2).booleanValue(true)) .andRespond(withSuccess()); - this.restTemplate.put(new URI("/composers"), this.people); - this.mockServer.verify(); + executeAndVerify(); } @Test @@ -186,6 +182,10 @@ public class XpathRequestMatchersIntegrationTests { .andExpect(xpath("/ns:people/performers/performer", NS).nodeCount(equalTo(2))) // Hamcrest.. .andRespond(withSuccess()); + executeAndVerify(); + } + + private void executeAndVerify() throws URISyntaxException { this.restTemplate.put(new URI("/composers"), this.people); this.mockServer.verify(); }