Merge branch '2.0.x'

This commit is contained in:
Andy Wilkinson
2022-01-13 12:37:09 +00:00
19 changed files with 675 additions and 453 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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,14 +21,12 @@ import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
/**
@@ -42,25 +40,21 @@ public class RequestHeadersSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void missingRequestHeader() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
equalTo("Headers with the following names were not found" + " in the request: [Accept]"));
new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description("one")))
.document(this.operationBuilder.request("http://localhost").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description("one")))
.document(this.operationBuilder.request("http://localhost").build()))
.withMessage("Headers with the following names were not found in the request: [Accept]");
}
@Test
public void undocumentedRequestHeaderAndMissingRequestHeader() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
endsWith("Headers with the following names were not found" + " in the request: [Accept]"));
new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description("one")))
.document(this.operationBuilder.request("http://localhost").header("X-Test", "test").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestHeadersSnippet(Arrays.asList(headerWithName("Accept").description("one")))
.document(this.operationBuilder.request("http://localhost").header("X-Test", "test").build()))
.withMessageEndingWith("Headers with the following names were not found in the request: [Accept]");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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,14 +21,12 @@ import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
/**
@@ -42,25 +40,21 @@ public class ResponseHeadersSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void missingResponseHeader() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
equalTo("Headers with the following names were not found" + " in the response: [Content-Type]"));
new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type").description("one")))
.document(this.operationBuilder.response().build());
assertThatExceptionOfType(SnippetException.class).isThrownBy(
() -> new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type").description("one")))
.document(this.operationBuilder.response().build()))
.withMessage("Headers with the following names were not found" + " in the response: [Content-Type]");
}
@Test
public void undocumentedResponseHeaderAndMissingResponseHeader() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
endsWith("Headers with the following names were not found" + " in the response: [Content-Type]"));
new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type").description("one")))
.document(this.operationBuilder.response().header("X-Test", "test").build());
assertThatExceptionOfType(SnippetException.class).isThrownBy(
() -> new ResponseHeadersSnippet(Arrays.asList(headerWithName("Content-Type").description("one")))
.document(this.operationBuilder.response().header("X-Test", "test").build()))
.withMessageEndingWith(
"Headers with the following names were not found in the response: [Content-Type]");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -22,13 +22,12 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Tests for failures when rendering {@link LinksSnippet} due to missing or undocumented
@@ -41,42 +40,40 @@ public class LinksSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void undocumentedLink() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Links with the following relations were not" + " documented: [foo]"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
Collections.<LinkDescriptor>emptyList()).document(this.operationBuilder.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
Collections.<LinkDescriptor>emptyList()).document(this.operationBuilder.build()))
.withMessage("Links with the following relations were not documented: [foo]");
}
@Test
public void missingLink() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
equalTo("Links with the following relations were not" + " found in the response: [foo]"));
new LinksSnippet(new StubLinkExtractor(), Arrays.asList(new LinkDescriptor("foo").description("bar")))
.document(this.operationBuilder.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new LinksSnippet(new StubLinkExtractor(),
Arrays.asList(new LinkDescriptor("foo").description("bar")))
.document(this.operationBuilder.build()))
.withMessage("Links with the following relations were not found in the response: [foo]");
}
@Test
public void undocumentedLinkAndMissingLink() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Links with the following relations were not"
+ " documented: [a]. Links with the following relations were not" + " found in the response: [foo]"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha")),
Arrays.asList(new LinkDescriptor("foo").description("bar"))).document(this.operationBuilder.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha")),
Arrays.asList(new LinkDescriptor("foo").description("bar")))
.document(this.operationBuilder.build()))
.withMessage("Links with the following relations were not documented: [a]. Links with the following"
+ " relations were not found in the response: [foo]");
}
@Test
public void linkWithNoDescription() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("No description was provided for the link with rel 'foo' and no"
+ " title was available from the link in the payload"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
Arrays.asList(new LinkDescriptor("foo"))).document(this.operationBuilder.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")),
Arrays.asList(new LinkDescriptor("foo"))).document(this.operationBuilder.build()))
.withMessage("No description was provided for the link with rel 'foo' and no title was available"
+ " from the link in the payload");
}
}

View File

@@ -19,12 +19,10 @@ package org.springframework.restdocs.operation;
import java.net.URI;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link QueryStringParser}.
@@ -33,9 +31,6 @@ import static org.hamcrest.CoreMatchers.equalTo;
*/
public class QueryStringParserTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private final QueryStringParser queryStringParser = new QueryStringParser();
@Test
@@ -76,9 +71,9 @@ public class QueryStringParserTests {
@Test
public void malformedParameter() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage(equalTo("The parameter 'a=apple=avocado' is malformed"));
this.queryStringParser.parse(URI.create("http://localhost?a=apple=avocado"));
assertThatIllegalArgumentException()
.isThrownBy(() -> this.queryStringParser.parse(URI.create("http://localhost?a=apple=avocado")))
.withMessage("The parameter 'a=apple=avocado' is malformed");
}
@Test

View File

@@ -23,10 +23,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.restdocs.testfixtures.OutputCapture;
import org.springframework.restdocs.testfixtures.OutputCaptureRule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.isEmptyString;
/**
* Tests for {@link PrettyPrintingContentModifier}.
@@ -37,7 +36,7 @@ import static org.hamcrest.Matchers.isEmptyString;
public class PrettyPrintingContentModifierTests {
@Rule
public OutputCapture outputCapture = new OutputCapture();
public OutputCaptureRule outputCapture = new OutputCaptureRule();
@Test
public void prettyPrintJson() throws Exception {
@@ -61,17 +60,17 @@ public class PrettyPrintingContentModifierTests {
@Test
public void nonJsonAndNonXmlContentIsHandledGracefully() throws Exception {
String content = "abcdefg";
this.outputCapture.expect(isEmptyString());
assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(), null))
.isEqualTo(content.getBytes());
assertThat(this.outputCapture).isEmpty();
}
@Test
public void nonJsonContentThatInitiallyLooksLikeJsonIsHandledGracefully() throws Exception {
String content = "\"abc\",\"def\"";
this.outputCapture.expect(isEmptyString());
assertThat(new PrettyPrintingContentModifier().modifyContent(content.getBytes(), null))
.isEqualTo(content.getBytes());
assertThat(this.outputCapture).isEmpty();
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -22,7 +22,6 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -30,9 +29,7 @@ import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
/**
@@ -46,148 +43,145 @@ public class RequestFieldsSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void undocumentedRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not" + " documented:"));
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").content("{\"a\": 5}").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").content("{\"a\": 5}").build()))
.withMessageStartingWith("The following parts of the payload were not documented:");
}
@Test
public void missingRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Fields with the following paths were not found" + " in the payload: [a.b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")))
.document(this.operationBuilder.request("http://localhost").content("{}").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")))
.document(this.operationBuilder.request("http://localhost").content("{}").build()))
.withMessage("Fields with the following paths were not found in the payload: [a.b]");
}
@Test
public void missingOptionalRequestFieldWithNoTypeProvided() throws IOException {
this.thrown.expect(FieldTypeRequiredException.class);
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one").optional()))
.document(this.operationBuilder.request("http://localhost").content("{ }").build());
assertThatExceptionOfType(FieldTypeRequiredException.class).isThrownBy(
() -> new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one").optional()))
.document(this.operationBuilder.request("http://localhost").content("{ }").build()));
}
@Test
public void undocumentedRequestFieldAndMissingRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not" + " documented:"));
this.thrown
.expectMessage(endsWith("Fields with the following paths were not found" + " in the payload: [a.b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")))
.document(this.operationBuilder.request("http://localhost").content("{ \"a\": { \"c\": 5 }}").build());
assertThatExceptionOfType(SnippetException.class).isThrownBy(
() -> new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one"))).document(
this.operationBuilder.request("http://localhost").content("{ \"a\": { \"c\": 5 }}").build()))
.withMessageStartingWith("The following parts of the payload were not documented:")
.withMessageEndingWith("Fields with the following paths were not found in the payload: [a.b]");
}
@Test
public void attemptToDocumentFieldsWithNoRequestBody() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Cannot document request fields as the request body is empty"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(this.operationBuilder.request("http://localhost").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(this.operationBuilder.request("http://localhost").build()))
.withMessage("Cannot document request fields as the request body is empty");
}
@Test
public void fieldWithExplicitTypeThatDoesNotMatchThePayload() throws IOException {
this.thrown.expect(FieldTypesDoNotMatchException.class);
this.thrown.expectMessage(
equalTo("The documented type of the field 'a' is" + " Object but the actual type is Number"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one").type(JsonFieldType.OBJECT)))
.document(this.operationBuilder.request("http://localhost").content("{ \"a\": 5 }").build());
assertThatExceptionOfType(FieldTypesDoNotMatchException.class)
.isThrownBy(() -> new RequestFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type(JsonFieldType.OBJECT))).document(
this.operationBuilder.request("http://localhost").content("{ \"a\": 5 }").build()))
.withMessage("The documented type of the field 'a' is Object but the actual type is Number");
}
@Test
public void fieldWithExplicitSpecificTypeThatActuallyVaries() throws IOException {
this.thrown.expect(FieldTypesDoNotMatchException.class);
this.thrown.expectMessage(
equalTo("The documented type of the field '[].a' is" + " Object but the actual type is Varies"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("[].a").description("one").type(JsonFieldType.OBJECT)))
.document(this.operationBuilder.request("http://localhost").content("[{ \"a\": 5 },{ \"a\": \"b\" }]")
.build());
assertThatExceptionOfType(FieldTypesDoNotMatchException.class)
.isThrownBy(() -> new RequestFieldsSnippet(
Arrays.asList(fieldWithPath("[].a").description("one").type(JsonFieldType.OBJECT)))
.document(this.operationBuilder.request("http://localhost")
.content("[{ \"a\": 5 },{ \"a\": \"b\" }]").build()))
.withMessage("The documented type of the field '[].a' is Object but the actual type is Varies");
}
@Test
public void undocumentedXmlRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not documented:"));
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()))
.withMessageStartingWith("The following parts of the payload were not documented:");
}
@Test
public void xmlDescendentsAreNotDocumentedByFieldDescriptor() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not documented:"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").type("a").description("one")))
.document(this.operationBuilder.request("http://localhost").content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(
() -> new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").type("a").description("one")))
.document(this.operationBuilder.request("http://localhost").content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()))
.withMessageStartingWith("The following parts of the payload were not documented:");
}
@Test
public void xmlRequestFieldWithNoType() throws IOException {
this.thrown.expect(FieldTypeRequiredException.class);
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(this.operationBuilder.request("http://localhost").content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(FieldTypeRequiredException.class)
.isThrownBy(() -> new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(this.operationBuilder.request("http://localhost").content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()));
}
@Test
public void missingXmlRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Fields with the following paths were not found" + " in the payload: [a/b]"));
new RequestFieldsSnippet(
Arrays.asList(fieldWithPath("a/b").description("one"), fieldWithPath("a").description("one")))
.document(this.operationBuilder.request("http://localhost").content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestFieldsSnippet(
Arrays.asList(fieldWithPath("a/b").description("one"), fieldWithPath("a").description("one")))
.document(this.operationBuilder.request("http://localhost").content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()))
.withMessage("Fields with the following paths were not found in the payload: [a/b]");
}
@Test
public void undocumentedXmlRequestFieldAndMissingXmlRequestField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not documented:"));
this.thrown
.expectMessage(endsWith("Fields with the following paths were not found" + " in the payload: [a/b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")))
.document(this.operationBuilder.request("http://localhost").content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")))
.document(this.operationBuilder.request("http://localhost").content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()))
.withMessageStartingWith("The following parts of the payload were not documented:")
.withMessageEndingWith("Fields with the following paths were not found in the payload: [a/b]");
}
@Test
public void unsupportedContent() throws IOException {
this.thrown.expect(PayloadHandlingException.class);
this.thrown.expectMessage(
equalTo("Cannot handle text/plain content as it could" + " not be parsed as JSON or XML"));
new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").content("Some plain text")
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE).build());
assertThatExceptionOfType(PayloadHandlingException.class)
.isThrownBy(() -> new RequestFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").content("Some plain text")
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE).build()))
.withMessage("Cannot handle text/plain content as it could not be parsed as JSON or XML");
}
@Test
public void nonOptionalFieldBeneathArrayThatIsSometimesNull() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
startsWith("Fields with the following paths were not found in the payload: " + "[a[].b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a[].b").description("one").type(JsonFieldType.NUMBER),
fieldWithPath("a[].c").description("two").type(JsonFieldType.NUMBER)))
.document(this.operationBuilder.request("http://localhost").content(
"{\"a\":[{\"b\": 1,\"c\": 2}, " + "{\"b\": null, \"c\": 2}," + " {\"b\": 1,\"c\": 2}]}")
.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestFieldsSnippet(
Arrays.asList(fieldWithPath("a[].b").description("one").type(JsonFieldType.NUMBER),
fieldWithPath("a[].c").description("two").type(JsonFieldType.NUMBER)))
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\":[{\"b\": 1,\"c\": 2}, " + "{\"b\": null, \"c\": 2},"
+ " {\"b\": 1,\"c\": 2}]}")
.build()))
.withMessageStartingWith("Fields with the following paths were not found in the payload: [a[].b]");
}
@Test
public void nonOptionalFieldBeneathArrayThatIsSometimesAbsent() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
startsWith("Fields with the following paths were not found in the payload: " + "[a[].b]"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a[].b").description("one").type(JsonFieldType.NUMBER),
fieldWithPath("a[].c").description("two").type(JsonFieldType.NUMBER)))
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\":[{\"b\": 1,\"c\": 2}, " + "{\"c\": 2}, {\"b\": 1,\"c\": 2}]}")
.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestFieldsSnippet(
Arrays.asList(fieldWithPath("a[].b").description("one").type(JsonFieldType.NUMBER),
fieldWithPath("a[].c").description("two").type(JsonFieldType.NUMBER)))
.document(this.operationBuilder.request("http://localhost")
.content("{\"a\":[{\"b\": 1,\"c\": 2}, "
+ "{\"c\": 2}, {\"b\": 1,\"c\": 2}]}")
.build()))
.withMessageStartingWith("Fields with the following paths were not found in the payload: [a[].b]");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -22,14 +22,12 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
/**
@@ -44,32 +42,32 @@ public class RequestPartFieldsSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void undocumentedRequestPartField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not documented:"));
new RequestPartFieldsSnippet("part", Collections.<FieldDescriptor>emptyList()).document(
this.operationBuilder.request("http://localhost").part("part", "{\"a\": 5}".getBytes()).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestPartFieldsSnippet("part", Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost")
.part("part", "{\"a\": 5}".getBytes()).build()))
.withMessageStartingWith("The following parts of the payload were not documented:");
}
@Test
public void missingRequestPartField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not documented:"));
new RequestPartFieldsSnippet("part", Arrays.asList(fieldWithPath("b").description("one"))).document(
this.operationBuilder.request("http://localhost").part("part", "{\"a\": 5}".getBytes()).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(
() -> new RequestPartFieldsSnippet("part", Arrays.asList(fieldWithPath("b").description("one")))
.document(this.operationBuilder.request("http://localhost")
.part("part", "{\"a\": 5}".getBytes()).build()))
.withMessageStartingWith("The following parts of the payload were not documented:");
}
@Test
public void missingRequestPart() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("A request part named 'another' was not found in the request"));
new RequestPartFieldsSnippet("another", Arrays.asList(fieldWithPath("a.b").description("one")))
.document(this.operationBuilder.request("http://localhost")
.part("part", "{\"a\": {\"b\": 5}}".getBytes()).build());
assertThatExceptionOfType(SnippetException.class).isThrownBy(
() -> new RequestPartFieldsSnippet("another", Arrays.asList(fieldWithPath("a.b").description("one")))
.document(this.operationBuilder.request("http://localhost")
.part("part", "{\"a\": {\"b\": 5}}".getBytes()).build()))
.withMessage("A request part named 'another' was not found in the request");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -22,7 +22,6 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -30,9 +29,7 @@ import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
/**
@@ -46,124 +43,125 @@ public class ResponseFieldsSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void attemptToDocumentFieldsWithNoResponseBody() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Cannot document response fields as the response body is empty"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(this.operationBuilder.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(this.operationBuilder.build()))
.withMessage("Cannot document response fields as the response body is empty");
}
@Test
public void fieldWithExplicitTypeThatDoesNotMatchThePayload() throws IOException {
this.thrown.expect(FieldTypesDoNotMatchException.class);
this.thrown.expectMessage(
equalTo("The documented type of the field 'a' is" + " Object but the actual type is Number"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one").type(JsonFieldType.OBJECT)))
.document(this.operationBuilder.response().content("{ \"a\": 5 }}").build());
assertThatExceptionOfType(FieldTypesDoNotMatchException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a").description("one").type(JsonFieldType.OBJECT)))
.document(this.operationBuilder.response().content("{ \"a\": 5 }}").build()))
.withMessage("The documented type of the field 'a' is Object but the actual type is Number");
}
@Test
public void fieldWithExplicitSpecificTypeThatActuallyVaries() throws IOException {
this.thrown.expect(FieldTypesDoNotMatchException.class);
this.thrown.expectMessage(
equalTo("The documented type of the field '[].a' is" + " Object but the actual type is Varies"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("[].a").description("one").type(JsonFieldType.OBJECT)))
.document(this.operationBuilder.response().content("[{ \"a\": 5 },{ \"a\": \"b\" }]").build());
assertThatExceptionOfType(FieldTypesDoNotMatchException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("[].a").description("one").type(JsonFieldType.OBJECT))).document(
this.operationBuilder.response().content("[{ \"a\": 5 },{ \"a\": \"b\" }]").build()))
.withMessage("The documented type of the field '[].a' is Object but the actual type is Varies");
}
@Test
public void undocumentedXmlResponseField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not" + " documented:"));
new ResponseFieldsSnippet(Collections.<FieldDescriptor>emptyList()).document(this.operationBuilder.response()
.content("<a><b>5</b></a>").header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.response().content("<a><b>5</b></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()))
.withMessageStartingWith("The following parts of the payload were not documented:");
}
@Test
public void missingXmlAttribute() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown
.expectMessage(equalTo("Fields with the following paths were not found" + " in the payload: [a/@id]"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one").type("b"),
fieldWithPath("a/@id").description("two").type("c")))
.document(this.operationBuilder.response().content("<a>foo</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(
() -> new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one").type("b"),
fieldWithPath("a/@id").description("two").type("c")))
.document(
this.operationBuilder.response().content("<a>foo</a>")
.header(HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_XML_VALUE)
.build()))
.withMessage("Fields with the following paths were not found in the payload: [a/@id]");
}
@Test
public void documentedXmlAttributesAreRemoved() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
equalTo(String.format("The following parts of the payload were not documented:" + "%n<a>bar</a>%n")));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/@id").description("one").type("a")))
.document(this.operationBuilder.response().content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class).isThrownBy(
() -> new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/@id").description("one").type("a")))
.document(this.operationBuilder.response().content("<a id=\"foo\">bar</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()))
.withMessage(String.format("The following parts of the payload were not documented:%n<a>bar</a>%n"));
}
@Test
public void xmlResponseFieldWithNoType() throws IOException {
this.thrown.expect(FieldTypeRequiredException.class);
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(this.operationBuilder.response().content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(FieldTypeRequiredException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(this.operationBuilder.response().content("<a>5</a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()));
}
@Test
public void missingXmlResponseField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Fields with the following paths were not found" + " in the payload: [a/b]"));
new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a/b").description("one"), fieldWithPath("a").description("one")))
.document(this.operationBuilder.response().content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a/b").description("one"), fieldWithPath("a").description("one")))
.document(this.operationBuilder.response().content("<a></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()))
.withMessage("Fields with the following paths were not found in the payload: [a/b]");
}
@Test
public void undocumentedXmlResponseFieldAndMissingXmlResponseField() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(startsWith("The following parts of the payload were not" + " documented:"));
this.thrown
.expectMessage(endsWith("Fields with the following paths were not found" + " in the payload: [a/b]"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")))
.document(this.operationBuilder.response().content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a/b").description("one")))
.document(this.operationBuilder.response().content("<a><c>5</c></a>")
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE).build()))
.withMessageStartingWith("The following parts of the payload were not documented:")
.withMessageEndingWith("Fields with the following paths were not found in the payload: [a/b]");
}
@Test
public void unsupportedContent() throws IOException {
this.thrown.expect(PayloadHandlingException.class);
this.thrown.expectMessage(
equalTo("Cannot handle text/plain content as it could" + " not be parsed as JSON or XML"));
new ResponseFieldsSnippet(Collections.<FieldDescriptor>emptyList()).document(this.operationBuilder.response()
.content("Some plain text").header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE).build());
assertThatExceptionOfType(PayloadHandlingException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(Collections.<FieldDescriptor>emptyList())
.document(this.operationBuilder.response().content("Some plain text")
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE).build()))
.withMessage("Cannot handle text/plain content as it could not be parsed as JSON or XML");
}
@Test
public void nonOptionalFieldBeneathArrayThatIsSometimesNull() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
startsWith("Fields with the following paths were not found in the payload: " + "[a[].b]"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a[].b").description("one").type(JsonFieldType.NUMBER),
fieldWithPath("a[].c").description("two").type(JsonFieldType.NUMBER)))
.document(this.operationBuilder.response().content(
"{\"a\":[{\"b\": 1,\"c\": 2}, " + "{\"b\": null, \"c\": 2}," + " {\"b\": 1,\"c\": 2}]}")
.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a[].b").description("one").type(JsonFieldType.NUMBER),
fieldWithPath("a[].c").description("two").type(JsonFieldType.NUMBER)))
.document(this.operationBuilder.response()
.content("{\"a\":[{\"b\": 1,\"c\": 2}, " + "{\"b\": null, \"c\": 2},"
+ " {\"b\": 1,\"c\": 2}]}")
.build()))
.withMessageStartingWith("Fields with the following paths were not found in the payload: [a[].b]");
}
@Test
public void nonOptionalFieldBeneathArrayThatIsSometimesAbsent() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
startsWith("Fields with the following paths were not found in the payload: " + "[a[].b]"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a[].b").description("one").type(JsonFieldType.NUMBER),
fieldWithPath("a[].c").description("two").type(JsonFieldType.NUMBER)))
.document(this.operationBuilder.response()
.content("{\"a\":[{\"b\": 1,\"c\": 2}, " + "{\"c\": 2}, {\"b\": 1,\"c\": 2}]}")
.build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new ResponseFieldsSnippet(
Arrays.asList(fieldWithPath("a[].b").description("one").type(JsonFieldType.NUMBER),
fieldWithPath("a[].c").description("two").type(JsonFieldType.NUMBER)))
.document(this.operationBuilder.response()
.content("{\"a\":[{\"b\": 1,\"c\": 2}, "
+ "{\"c\": 2}, {\"b\": 1,\"c\": 2}]}")
.build()))
.withMessageStartingWith("Fields with the following paths were not found in the payload: [a[].b]");
}
}

View File

@@ -22,14 +22,13 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.generate.RestDocumentationGenerator;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
/**
@@ -43,35 +42,33 @@ public class PathParametersSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void undocumentedPathParameter() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Path parameters with the following names were" + " not documented: [a]"));
new PathParametersSnippet(Collections.<ParameterDescriptor>emptyList()).document(this.operationBuilder
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/{a}/").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new PathParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(this.operationBuilder
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/{a}/").build()))
.withMessage("Path parameters with the following names were not documented: [a]");
}
@Test
public void missingPathParameter() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
equalTo("Path parameters with the following names were" + " not found in the request: [a]"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one"))).document(
this.operationBuilder.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/").build()))
.withMessage("Path parameters with the following names were not found in the request: [a]");
}
@Test
public void undocumentedAndMissingPathParameters() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Path parameters with the following names were"
+ " not documented: [b]. Path parameters with the following"
+ " names were not found in the request: [a]"));
new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/{b}").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new PathParametersSnippet(Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder
.attribute(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE, "/{b}").build()))
.withMessage(
"Path parameters with the following names were not documented: [b]. Path parameters with the"
+ " following names were not found in the request: [a]");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -22,13 +22,12 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
/**
@@ -42,34 +41,29 @@ public class RequestParametersSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void undocumentedParameter() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Request parameters with the following names were" + " not documented: [a]"));
new RequestParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").param("a", "alpha").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestParametersSnippet(Collections.<ParameterDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").param("a", "alpha").build()))
.withMessage("Request parameters with the following names were not documented: [a]");
}
@Test
public void missingParameter() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
equalTo("Request parameters with the following names were" + " not found in the request: [a]"));
new RequestParametersSnippet(Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder.request("http://localhost").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestParametersSnippet(Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder.request("http://localhost").build()))
.withMessage("Request parameters with the following names were not found in the request: [a]");
}
@Test
public void undocumentedAndMissingParameters() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Request parameters with the following names were"
+ " not documented: [b]. Request parameters with the following"
+ " names were not found in the request: [a]"));
new RequestParametersSnippet(Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder.request("http://localhost").param("b", "bravo").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestParametersSnippet(Arrays.asList(parameterWithName("a").description("one")))
.document(this.operationBuilder.request("http://localhost").param("b", "bravo").build()))
.withMessage("Request parameters with the following names were not documented: [b]. Request parameters"
+ " with the following names were not found in the request: [a]");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -22,13 +22,12 @@ import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.restdocs.snippet.SnippetException;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.restdocs.testfixtures.OperationBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.restdocs.request.RequestDocumentation.partWithName;
/**
@@ -42,34 +41,29 @@ public class RequestPartsSnippetFailureTests {
@Rule
public OperationBuilder operationBuilder = new OperationBuilder(TemplateFormats.asciidoctor());
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void undocumentedPart() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo("Request parts with the following names were" + " not documented: [a]"));
new RequestPartsSnippet(Collections.<RequestPartDescriptor>emptyList())
.document(this.operationBuilder.request("http://localhost").part("a", "alpha".getBytes()).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestPartsSnippet(Collections.<RequestPartDescriptor>emptyList()).document(
this.operationBuilder.request("http://localhost").part("a", "alpha".getBytes()).build()))
.withMessage("Request parts with the following names were not documented: [a]");
}
@Test
public void missingPart() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(
equalTo("Request parts with the following names were" + " not found in the request: [a]"));
new RequestPartsSnippet(Arrays.asList(partWithName("a").description("one")))
.document(this.operationBuilder.request("http://localhost").build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestPartsSnippet(Arrays.asList(partWithName("a").description("one")))
.document(this.operationBuilder.request("http://localhost").build()))
.withMessage("Request parts with the following names were not found in the request: [a]");
}
@Test
public void undocumentedAndMissingParts() throws IOException {
this.thrown.expect(SnippetException.class);
this.thrown.expectMessage(equalTo(
"Request parts with the following names were" + " not documented: [b]. Request parts with the following"
+ " names were not found in the request: [a]"));
new RequestPartsSnippet(Arrays.asList(partWithName("a").description("one")))
.document(this.operationBuilder.request("http://localhost").part("b", "bravo".getBytes()).build());
assertThatExceptionOfType(SnippetException.class)
.isThrownBy(() -> new RequestPartsSnippet(Arrays.asList(partWithName("a").description("one"))).document(
this.operationBuilder.request("http://localhost").part("b", "bravo".getBytes()).build()))
.withMessage("Request parts with the following names were not documented: [b]. Request parts with the"
+ " following names were not found in the request: [a]");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2022 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,14 +21,12 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link TemplateResourceResolver}.
@@ -37,9 +35,6 @@ import static org.hamcrest.CoreMatchers.equalTo;
*/
public class StandardTemplateResourceResolverTests {
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final TemplateResourceResolver resolver = new StandardTemplateResourceResolver(
TemplateFormats.asciidoctor());
@@ -101,16 +96,10 @@ public class StandardTemplateResourceResolverTests {
@Test
public void failsIfCustomAndDefaultSnippetsDoNotExist() throws Exception {
this.thrown.expect(IllegalStateException.class);
this.thrown.expectMessage(equalTo("Template named 'test' could not be resolved"));
doWithThreadContextClassLoader(this.classLoader, new Callable<Resource>() {
@Override
public Resource call() {
return StandardTemplateResourceResolverTests.this.resolver.resolveTemplateResource("test");
}
});
assertThatIllegalStateException()
.isThrownBy(() -> doWithThreadContextClassLoader(this.classLoader,
() -> StandardTemplateResourceResolverTests.this.resolver.resolveTemplateResource("test")))
.withMessage("Template named 'test' could not be resolved");
}
private <T> T doWithThreadContextClassLoader(ClassLoader classLoader, Callable<T> action) throws Exception {