Skip to content

Commit 7cb91f9

Browse files
committed
Remove use of Spring Boot from REST Assured module's integration tests
Closes gh-360
1 parent a19aef7 commit 7cb91f9

File tree

4 files changed

+171
-135
lines changed

4 files changed

+171
-135
lines changed

Diff for: spring-restdocs-restassured/build.gradle

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ dependencies {
99
compile project(':spring-restdocs-core')
1010
compile 'com.jayway.restassured:rest-assured'
1111

12+
testCompile 'org.apache.tomcat.embed:tomcat-embed-core:8.5.11'
1213
testCompile 'org.mockito:mockito-core'
1314
testCompile 'org.hamcrest:hamcrest-library'
14-
testCompile 'org.springframework.hateoas:spring-hateoas'
15-
testCompile 'org.springframework.boot:spring-boot-starter-web:1.3.6.RELEASE'
16-
testCompile 'org.springframework:spring-test'
1715
testCompile project(path: ':spring-restdocs-core', configuration: 'testArtifacts')
18-
testRuntime 'commons-logging:commons-logging:1.2'
1916
}
2017

2118
test {

Diff for: spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/RestAssuredRequestConverterTests.java

+29-57
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,25 +28,15 @@
2828
import com.jayway.restassured.RestAssured;
2929
import com.jayway.restassured.specification.FilterableRequestSpecification;
3030
import com.jayway.restassured.specification.RequestSpecification;
31+
import org.junit.ClassRule;
3132
import org.junit.Rule;
3233
import org.junit.Test;
3334
import org.junit.rules.ExpectedException;
34-
import org.junit.runner.RunWith;
3535

36-
import org.springframework.beans.factory.annotation.Value;
37-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
38-
import org.springframework.boot.test.IntegrationTest;
39-
import org.springframework.boot.test.SpringApplicationConfiguration;
40-
import org.springframework.context.annotation.Configuration;
4136
import org.springframework.http.HttpMethod;
4237
import org.springframework.http.MediaType;
4338
import org.springframework.restdocs.operation.OperationRequest;
4439
import org.springframework.restdocs.operation.OperationRequestPart;
45-
import org.springframework.restdocs.restassured.RestAssuredRequestConverterTests.TestApplication;
46-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
47-
import org.springframework.test.context.web.WebAppConfiguration;
48-
import org.springframework.web.bind.annotation.RequestMapping;
49-
import org.springframework.web.bind.annotation.RestController;
5040

5141
import static org.hamcrest.CoreMatchers.equalTo;
5242
import static org.hamcrest.CoreMatchers.is;
@@ -57,33 +47,29 @@
5747
*
5848
* @author Andy Wilkinson
5949
*/
60-
@RunWith(SpringJUnit4ClassRunner.class)
61-
@SpringApplicationConfiguration(classes = TestApplication.class)
62-
@WebAppConfiguration
63-
@IntegrationTest("server.port=0")
6450
public class RestAssuredRequestConverterTests {
6551

52+
@ClassRule
53+
public static TomcatServer tomcat = new TomcatServer();
54+
6655
@Rule
6756
public final ExpectedException thrown = ExpectedException.none();
6857

6958
private final RestAssuredRequestConverter factory = new RestAssuredRequestConverter();
7059

71-
@Value("${local.server.port}")
72-
private int port;
73-
7460
@Test
7561
public void requestUri() {
76-
RequestSpecification requestSpec = RestAssured.given().port(this.port);
62+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
7763
requestSpec.get("/foo/bar");
7864
OperationRequest request = this.factory
7965
.convert((FilterableRequestSpecification) requestSpec);
80-
assertThat(request.getUri(),
81-
is(equalTo(URI.create("http://localhost:" + this.port + "/foo/bar"))));
66+
assertThat(request.getUri(), is(equalTo(
67+
URI.create("http://localhost:" + tomcat.getPort() + "/foo/bar"))));
8268
}
8369

8470
@Test
8571
public void requestMethod() {
86-
RequestSpecification requestSpec = RestAssured.given().port(this.port);
72+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
8773
requestSpec.head("/foo/bar");
8874
OperationRequest request = this.factory
8975
.convert((FilterableRequestSpecification) requestSpec);
@@ -92,7 +78,7 @@ public void requestMethod() {
9278

9379
@Test
9480
public void queryStringParameters() {
95-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
81+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
9682
.queryParam("foo", "bar");
9783
requestSpec.get("/");
9884
OperationRequest request = this.factory
@@ -103,7 +89,7 @@ public void queryStringParameters() {
10389

10490
@Test
10591
public void queryStringFromUrlParameters() {
106-
RequestSpecification requestSpec = RestAssured.given().port(this.port);
92+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort());
10793
requestSpec.get("/?foo=bar");
10894
OperationRequest request = this.factory
10995
.convert((FilterableRequestSpecification) requestSpec);
@@ -113,7 +99,7 @@ public void queryStringFromUrlParameters() {
11399

114100
@Test
115101
public void formParameters() {
116-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
102+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
117103
.formParameter("foo", "bar");
118104
requestSpec.get("/");
119105
OperationRequest request = this.factory
@@ -124,7 +110,7 @@ public void formParameters() {
124110

125111
@Test
126112
public void requestParameters() {
127-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
113+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
128114
.parameter("foo", "bar");
129115
requestSpec.get("/");
130116
OperationRequest request = this.factory
@@ -135,7 +121,7 @@ public void requestParameters() {
135121

136122
@Test
137123
public void headers() {
138-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
124+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
139125
.header("Foo", "bar");
140126
requestSpec.get("/");
141127
OperationRequest request = this.factory
@@ -144,15 +130,15 @@ public void headers() {
144130
assertThat(request.getHeaders().get("Foo"), is(equalTo(Arrays.asList("bar"))));
145131
assertThat(request.getHeaders().get("Accept"), is(equalTo(Arrays.asList("*/*"))));
146132
assertThat(request.getHeaders().get("Host"),
147-
is(equalTo(Arrays.asList("localhost:" + this.port))));
133+
is(equalTo(Arrays.asList("localhost:" + tomcat.getPort()))));
148134
}
149135

150136
@Test
151137
public void multipart() {
152-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
138+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
153139
.multiPart("a", "a.txt", "alpha", null)
154140
.multiPart("b", new ObjectBody("bar"), "application/json");
155-
requestSpec.post().then().statusCode(200);
141+
requestSpec.post();
156142
OperationRequest request = this.factory
157143
.convert((FilterableRequestSpecification) requestSpec);
158144
Collection<OperationRequestPart> parts = request.getParts();
@@ -174,15 +160,15 @@ public void multipart() {
174160
@Test
175161
public void byteArrayBody() {
176162
RequestSpecification requestSpec = RestAssured.given().body("body".getBytes())
177-
.port(this.port);
163+
.port(tomcat.getPort());
178164
requestSpec.post();
179165
this.factory.convert((FilterableRequestSpecification) requestSpec);
180166
}
181167

182168
@Test
183169
public void stringBody() {
184170
RequestSpecification requestSpec = RestAssured.given().body("body")
185-
.port(this.port);
171+
.port(tomcat.getPort());
186172
requestSpec.post();
187173
OperationRequest request = this.factory
188174
.convert((FilterableRequestSpecification) requestSpec);
@@ -192,7 +178,7 @@ public void stringBody() {
192178
@Test
193179
public void objectBody() {
194180
RequestSpecification requestSpec = RestAssured.given().body(new ObjectBody("bar"))
195-
.port(this.port);
181+
.port(tomcat.getPort());
196182
requestSpec.post();
197183
OperationRequest request = this.factory
198184
.convert((FilterableRequestSpecification) requestSpec);
@@ -203,7 +189,7 @@ public void objectBody() {
203189
public void byteArrayInputStreamBody() {
204190
RequestSpecification requestSpec = RestAssured.given()
205191
.body(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4 }))
206-
.port(this.port);
192+
.port(tomcat.getPort());
207193
requestSpec.post();
208194
OperationRequest request = this.factory
209195
.convert((FilterableRequestSpecification) requestSpec);
@@ -213,7 +199,7 @@ public void byteArrayInputStreamBody() {
213199
@Test
214200
public void fileBody() {
215201
RequestSpecification requestSpec = RestAssured.given()
216-
.body(new File("src/test/resources/body.txt")).port(this.port);
202+
.body(new File("src/test/resources/body.txt")).port(tomcat.getPort());
217203
requestSpec.post();
218204
OperationRequest request = this.factory
219205
.convert((FilterableRequestSpecification) requestSpec);
@@ -224,7 +210,7 @@ public void fileBody() {
224210
public void fileInputStreamBody() throws FileNotFoundException {
225211
FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt");
226212
RequestSpecification requestSpec = RestAssured.given().body(inputStream)
227-
.port(this.port);
213+
.port(tomcat.getPort());
228214
requestSpec.post();
229215
this.thrown.expect(IllegalStateException.class);
230216
this.thrown.expectMessage("Cannot read content from input stream " + inputStream
@@ -234,7 +220,7 @@ public void fileInputStreamBody() throws FileNotFoundException {
234220

235221
@Test
236222
public void multipartWithByteArrayInputStreamBody() {
237-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
223+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
238224
.multiPart("foo", "foo.txt", new ByteArrayInputStream("foo".getBytes()));
239225
requestSpec.post();
240226
OperationRequest request = this.factory
@@ -245,7 +231,7 @@ public void multipartWithByteArrayInputStreamBody() {
245231

246232
@Test
247233
public void multipartWithStringBody() {
248-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
234+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
249235
.multiPart("control", "foo");
250236
requestSpec.post();
251237
OperationRequest request = this.factory
@@ -256,7 +242,7 @@ public void multipartWithStringBody() {
256242

257243
@Test
258244
public void multipartWithByteArrayBody() {
259-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
245+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
260246
.multiPart("control", "file", "foo".getBytes());
261247
requestSpec.post();
262248
OperationRequest request = this.factory
@@ -267,7 +253,7 @@ public void multipartWithByteArrayBody() {
267253

268254
@Test
269255
public void multipartWithFileBody() {
270-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
256+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
271257
.multiPart(new File("src/test/resources/body.txt"));
272258
requestSpec.post();
273259
OperationRequest request = this.factory
@@ -279,7 +265,7 @@ public void multipartWithFileBody() {
279265
@Test
280266
public void multipartWithFileInputStreamBody() throws FileNotFoundException {
281267
FileInputStream inputStream = new FileInputStream("src/test/resources/body.txt");
282-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
268+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
283269
.multiPart("foo", "foo.txt", inputStream);
284270
requestSpec.post();
285271
this.thrown.expect(IllegalStateException.class);
@@ -290,7 +276,7 @@ public void multipartWithFileInputStreamBody() throws FileNotFoundException {
290276

291277
@Test
292278
public void multipartWithObjectBody() {
293-
RequestSpecification requestSpec = RestAssured.given().port(this.port)
279+
RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort())
294280
.multiPart("control", new ObjectBody("bar"));
295281
requestSpec.post();
296282
OperationRequest request = this.factory
@@ -299,20 +285,6 @@ public void multipartWithObjectBody() {
299285
is(equalTo("{\"foo\":\"bar\"}")));
300286
}
301287

302-
/**
303-
* Minimal test web application.
304-
*/
305-
@Configuration
306-
@EnableAutoConfiguration
307-
@RestController
308-
static class TestApplication {
309-
310-
@RequestMapping("/")
311-
void handle() {
312-
}
313-
314-
}
315-
316288
/**
317289
* Sample object body to verify JSON serialization.
318290
*/

0 commit comments

Comments
 (0)