Polish MockRestServiceServer client tests

This commit is contained in:
Rossen Stoyanchev 2018-03-15 10:30:42 -04:00
parent 8d51d6769b
commit f7e75a5b82
5 changed files with 56 additions and 30 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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();
}