Document AssertJ support for MockMvc
This commit restructures the section on MockMvc so that the anchors are easier to read. The standard integration has moved to a Hamcrest Integration section at the same level as HtmlUnit Integration, and a new AssertJ Integration section has been created. Closes gh-32454
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterassertions;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class HotelControllerTests {
|
||||
|
||||
private final MockMvcTester mockMvc = MockMvcTester.of(new HotelController());
|
||||
|
||||
|
||||
void getHotel() {
|
||||
// tag::get[]
|
||||
assertThat(mockMvc.get().uri("/hotels/{id}", 42))
|
||||
.hasStatusOk()
|
||||
.hasContentTypeCompatibleWith(MediaType.APPLICATION_JSON)
|
||||
.bodyJson().isLenientlyEqualTo("sample/hotel-42.json");
|
||||
// end::get[]
|
||||
}
|
||||
|
||||
|
||||
void getHotelInvalid() {
|
||||
// tag::failure[]
|
||||
assertThat(mockMvc.get().uri("/hotels/{id}", -1))
|
||||
.hasFailed()
|
||||
.hasStatus(HttpStatus.BAD_REQUEST)
|
||||
.failure().hasMessageContaining("Identifier should be positive");
|
||||
// end::failure[]
|
||||
}
|
||||
|
||||
static class HotelController {}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterassertionsjson;
|
||||
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
|
||||
class FamilyControllerTests {
|
||||
|
||||
private final MockMvcTester mockMvc = MockMvcTester.of(new FamilyController());
|
||||
|
||||
|
||||
void extractingPathAsMap() {
|
||||
// tag::extract-asmap[]
|
||||
assertThat(mockMvc.get().uri("/family")).bodyJson()
|
||||
.extractingPath("$.members[0]")
|
||||
.asMap()
|
||||
.contains(entry("name", "Homer"));
|
||||
// end::extract-asmap[]
|
||||
}
|
||||
|
||||
void extractingPathAndConvertWithType() {
|
||||
// tag::extract-convert[]
|
||||
assertThat(mockMvc.get().uri("/family")).bodyJson()
|
||||
.extractingPath("$.members[0]")
|
||||
.convertTo(Member.class)
|
||||
.satisfies(member -> assertThat(member.name).isEqualTo("Homer"));
|
||||
// end::extract-convert[]
|
||||
}
|
||||
|
||||
void extractingPathAndConvertWithAssertFactory() {
|
||||
// tag::extract-convert-assert-factory[]
|
||||
assertThat(mockMvc.get().uri("/family")).bodyJson()
|
||||
.extractingPath("$.members")
|
||||
.convertTo(InstanceOfAssertFactories.list(Member.class))
|
||||
.hasSize(5)
|
||||
.element(0).satisfies(member -> assertThat(member.name).isEqualTo("Homer"));
|
||||
// end::extract-convert-assert-factory[]
|
||||
}
|
||||
|
||||
void assertTheSimpsons() {
|
||||
// tag::assert-file[]
|
||||
assertThat(mockMvc.get().uri("/family")).bodyJson()
|
||||
.isStrictlyEqualTo("sample/simpsons.json");
|
||||
// end::assert-file[]
|
||||
}
|
||||
|
||||
static class FamilyController {}
|
||||
|
||||
record Member(String name) {}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterintegration;
|
||||
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
public class HotelControllerTests {
|
||||
|
||||
private final MockMvcTester mockMvc = MockMvcTester.of(new HotelController());
|
||||
|
||||
|
||||
void perform() {
|
||||
// tag::perform[]
|
||||
// Static import on MockMvcRequestBuilders.get
|
||||
assertThat(mockMvc.perform(get("/hotels/{id}", 42)))
|
||||
.hasStatusOk();
|
||||
// end::perform[]
|
||||
}
|
||||
|
||||
void performWithCustomMatcher() {
|
||||
// tag::matches[]
|
||||
// Static import on MockMvcResultMatchers.status
|
||||
assertThat(mockMvc.get().uri("/hotels/{id}", 42))
|
||||
.matches(status().isOk());
|
||||
// end::matches[]
|
||||
}
|
||||
|
||||
static class HotelController {}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterrequests;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
import org.springframework.test.web.servlet.assertj.MvcTestResult;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class HotelControllerTests {
|
||||
|
||||
private final MockMvcTester mockMvc = MockMvcTester.of(new HotelController());
|
||||
|
||||
|
||||
void createHotel() {
|
||||
// tag::post[]
|
||||
assertThat(mockMvc.post().uri("/hotels/{id}", 42).accept(MediaType.APPLICATION_JSON))
|
||||
. // ...
|
||||
// end::post[]
|
||||
hasStatusOk();
|
||||
}
|
||||
|
||||
void createHotelMultipleAssertions() {
|
||||
// tag::post-exchange[]
|
||||
MvcTestResult result = mockMvc.post().uri("/hotels/{id}", 42)
|
||||
.accept(MediaType.APPLICATION_JSON).exchange();
|
||||
assertThat(result). // ...
|
||||
// end::post-exchange[]
|
||||
hasStatusOk();
|
||||
}
|
||||
|
||||
void queryParameters() {
|
||||
// tag::query-parameters[]
|
||||
assertThat(mockMvc.get().uri("/hotels?thing={thing}", "somewhere"))
|
||||
. // ...
|
||||
// end::query-parameters[]
|
||||
hasStatusOk();
|
||||
}
|
||||
|
||||
void parameters() {
|
||||
// tag::parameters[]
|
||||
assertThat(mockMvc.get().uri("/hotels").param("thing", "somewhere"))
|
||||
. // ...
|
||||
// end::parameters[]
|
||||
hasStatusOk();
|
||||
}
|
||||
|
||||
static class HotelController {}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterrequestsasync;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class AsyncControllerTests {
|
||||
|
||||
private final MockMvcTester mockMvc = MockMvcTester.of(new AsyncController());
|
||||
|
||||
void asyncExchangeWithCustomTimeToWait() {
|
||||
// tag::duration[]
|
||||
assertThat(mockMvc.get().uri("/compute").exchange(Duration.ofSeconds(5)))
|
||||
. // ...
|
||||
// end::duration[]
|
||||
hasStatusOk();
|
||||
}
|
||||
|
||||
static class AsyncController {}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterrequestsmultipart;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class MultipartControllerTests {
|
||||
|
||||
private final MockMvcTester mockMvc = MockMvcTester.of(new MultipartController());
|
||||
|
||||
void multiPart() {
|
||||
// tag::snippet[]
|
||||
assertThat(mockMvc.post().uri("/upload").multipart()
|
||||
.file("file1.txt", "Hello".getBytes(StandardCharsets.UTF_8))
|
||||
.file("file2.txt", "World".getBytes(StandardCharsets.UTF_8)))
|
||||
. // ...
|
||||
// end::snippet[]
|
||||
hasStatusOk();
|
||||
}
|
||||
|
||||
static class MultipartController {}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterrequestspaths;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
|
||||
/**
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class HotelControllerTests {
|
||||
|
||||
private final MockMvcTester mockMvc = MockMvcTester.of(new HotelController());
|
||||
|
||||
void contextAndServletPaths() {
|
||||
// tag::context-servlet-paths[]
|
||||
assertThat(mockMvc.get().uri("/app/main/hotels/{id}", 42)
|
||||
.contextPath("/app").servletPath("/main"))
|
||||
. // ...
|
||||
// end::context-servlet-paths[]
|
||||
hasStatusOk();
|
||||
}
|
||||
|
||||
void configureMockMvcTesterWithDefaultSettings() {
|
||||
// tag::default-customizations[]
|
||||
MockMvcTester mockMvc = MockMvcTester.of(List.of(new HotelController()),
|
||||
builder -> builder.defaultRequest(get("/")
|
||||
.contextPath("/app").servletPath("/main")
|
||||
.accept(MediaType.APPLICATION_JSON)).build());
|
||||
// end::default-customizations[]
|
||||
}
|
||||
|
||||
|
||||
static class HotelController {}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup;
|
||||
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class AccountController {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
// tag::snippet[]
|
||||
@SpringJUnitWebConfig(ApplicationWebConfiguration.class)
|
||||
class AccountControllerIntegrationTests {
|
||||
|
||||
private final MockMvcTester mockMvc;
|
||||
|
||||
AccountControllerIntegrationTests(@Autowired WebApplicationContext wac) {
|
||||
this.mockMvc = MockMvcTester.from(wac);
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
// end::snippet[]
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup;
|
||||
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
// tag::snippet[]
|
||||
public class AccountControllerStandaloneTests {
|
||||
|
||||
private final MockMvcTester mockMvc = MockMvcTester.of(new AccountController());
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
// end::snippet[]
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class ApplicationWebConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup.converter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup.ApplicationWebConfiguration;
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
// tag::snippet[]
|
||||
@SpringJUnitWebConfig(ApplicationWebConfiguration.class)
|
||||
class AccountControllerIntegrationTests {
|
||||
|
||||
private final MockMvcTester mockMvc;
|
||||
|
||||
AccountControllerIntegrationTests(@Autowired WebApplicationContext wac) {
|
||||
this.mockMvc = MockMvcTester.from(wac).withHttpMessageConverters(
|
||||
List.of(wac.getBean(AbstractJackson2HttpMessageConverter.class)));
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
// end::snippet[]
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterassertions
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
|
||||
class HotelControllerTests {
|
||||
|
||||
private val mockMvc = MockMvcTester.of(HotelController())
|
||||
|
||||
fun getHotel() {
|
||||
// tag::get[]
|
||||
assertThat(mockMvc.get().uri("/hotels/{id}", 42))
|
||||
.hasStatusOk()
|
||||
.hasContentTypeCompatibleWith(MediaType.APPLICATION_JSON)
|
||||
.bodyJson().isLenientlyEqualTo("sample/hotel-42.json")
|
||||
// end::get[]
|
||||
}
|
||||
|
||||
|
||||
fun getHotelInvalid() {
|
||||
// tag::failure[]
|
||||
assertThat(mockMvc.get().uri("/hotels/{id}", -1))
|
||||
.hasFailed()
|
||||
.hasStatus(HttpStatus.BAD_REQUEST)
|
||||
.failure().hasMessageContaining("Identifier should be positive")
|
||||
// end::failure[]
|
||||
}
|
||||
|
||||
class HotelController
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterassertionsjson
|
||||
|
||||
import org.assertj.core.api.Assertions.*
|
||||
import org.assertj.core.api.InstanceOfAssertFactories
|
||||
import org.assertj.core.api.ThrowingConsumer
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class FamilyControllerTests {
|
||||
|
||||
private val mockMvc = MockMvcTester.of(FamilyController())
|
||||
|
||||
|
||||
fun extractingPathAsMap() {
|
||||
// tag::extract-asmap[]
|
||||
assertThat(mockMvc.get().uri("/family")).bodyJson()
|
||||
.extractingPath("$.members[0]")
|
||||
.asMap()
|
||||
.contains(entry("name", "Homer"))
|
||||
// end::extract-asmap[]
|
||||
}
|
||||
|
||||
fun extractingPathAndConvertWithType() {
|
||||
// tag::extract-convert[]
|
||||
assertThat(mockMvc.get().uri("/family")).bodyJson()
|
||||
.extractingPath("$.members[0]")
|
||||
.convertTo(Member::class.java)
|
||||
.satisfies(ThrowingConsumer { member: Member ->
|
||||
assertThat(member.name).isEqualTo("Homer")
|
||||
})
|
||||
// end::extract-convert[]
|
||||
}
|
||||
|
||||
fun extractingPathAndConvertWithAssertFactory() {
|
||||
// tag::extract-convert-assert-factory[]
|
||||
assertThat(mockMvc.get().uri("/family")).bodyJson()
|
||||
.extractingPath("$.members")
|
||||
.convertTo(InstanceOfAssertFactories.list(Member::class.java))
|
||||
.hasSize(5)
|
||||
.element(0).satisfies(ThrowingConsumer { member: Member ->
|
||||
assertThat(member.name).isEqualTo("Homer")
|
||||
})
|
||||
// end::extract-convert-assert-factory[]
|
||||
}
|
||||
|
||||
fun assertTheSimpsons() {
|
||||
// tag::assert-file[]
|
||||
assertThat(mockMvc.get().uri("/family")).bodyJson()
|
||||
.isStrictlyEqualTo("sample/simpsons.json")
|
||||
// end::assert-file[]
|
||||
}
|
||||
|
||||
class FamilyController
|
||||
|
||||
@JvmRecord
|
||||
data class Member(val name: String)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterintegration
|
||||
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.assertj.core.api.Assertions.*
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class HotelController {
|
||||
|
||||
private val mockMvc = MockMvcTester.of(HotelController())
|
||||
|
||||
|
||||
fun perform() {
|
||||
// tag::perform[]
|
||||
// Static import on MockMvcRequestBuilders.get
|
||||
assertThat(mockMvc.perform(get("/hotels/{id}",42)))
|
||||
.hasStatusOk()
|
||||
// end::perform[]
|
||||
}
|
||||
|
||||
fun performWithCustomMatcher() {
|
||||
// tag::perform[]
|
||||
// Static import on MockMvcResultMatchers.status
|
||||
assertThat(mockMvc.get().uri("/hotels/{id}", 42))
|
||||
.matches(status().isOk())
|
||||
// end::perform[]
|
||||
}
|
||||
|
||||
class HotelController
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterrequests
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
|
||||
class HotelControllerTests {
|
||||
|
||||
private val mockMvc = MockMvcTester.of(HotelController())
|
||||
|
||||
fun createHotel() {
|
||||
// tag::post[]
|
||||
assertThat(mockMvc.post().uri("/hotels/{id}", 42).accept(MediaType.APPLICATION_JSON))
|
||||
. // ...
|
||||
// end::post[]
|
||||
hasStatusOk()
|
||||
}
|
||||
|
||||
fun createHotelMultipleAssertions() {
|
||||
// tag::post-exchange[]
|
||||
val result = mockMvc.post().uri("/hotels/{id}", 42)
|
||||
.accept(MediaType.APPLICATION_JSON).exchange()
|
||||
assertThat(result)
|
||||
. // ...
|
||||
// end::post-exchange[]
|
||||
hasStatusOk()
|
||||
}
|
||||
|
||||
fun queryParameters() {
|
||||
// tag::query-parameters[]
|
||||
assertThat(mockMvc.get().uri("/hotels?thing={thing}", "somewhere"))
|
||||
. // ...
|
||||
//end::query-parameters[]
|
||||
hasStatusOk()
|
||||
}
|
||||
|
||||
fun parameters() {
|
||||
// tag::parameters[]
|
||||
assertThat(mockMvc.get().uri("/hotels").param("thing", "somewhere"))
|
||||
. // ...
|
||||
// end::parameters[]
|
||||
hasStatusOk()
|
||||
}
|
||||
|
||||
class HotelController
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterrequestsasync
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
import java.time.Duration
|
||||
|
||||
class AsyncControllerTests {
|
||||
|
||||
private val mockMvc = MockMvcTester.of(AsyncController())
|
||||
|
||||
fun asyncExchangeWithCustomTimeToWait() {
|
||||
// tag::duration[]
|
||||
assertThat(mockMvc.get().uri("/compute").exchange(Duration.ofSeconds(5)))
|
||||
. // ...
|
||||
// end::duration[]
|
||||
hasStatusOk()
|
||||
}
|
||||
|
||||
class AsyncController
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterrequestsmultipart
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class MultipartControllerTests {
|
||||
|
||||
private val mockMvc = MockMvcTester.of(MultipartController())
|
||||
|
||||
fun multiPart() {
|
||||
// tag::snippet[]
|
||||
assertThat(mockMvc.post().uri("/upload").multipart()
|
||||
.file("file1.txt", "Hello".toByteArray(StandardCharsets.UTF_8))
|
||||
.file("file2.txt", "World".toByteArray(StandardCharsets.UTF_8)))
|
||||
. // ...
|
||||
// end::snippet[]
|
||||
hasStatusOk()
|
||||
}
|
||||
|
||||
class MultipartController
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctesterrequestspaths
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||
import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder
|
||||
|
||||
class HotelControllerTests {
|
||||
|
||||
private val mockMvc = MockMvcTester.of(HotelController())
|
||||
|
||||
fun contextAndServletPaths() {
|
||||
// tag::context-servlet-paths[]
|
||||
assertThat(mockMvc.get().uri("/app/main/hotels/{id}", 42)
|
||||
.contextPath("/app").servletPath("/main"))
|
||||
. // ...
|
||||
// end::context-servlet-paths[]
|
||||
hasStatusOk()
|
||||
}
|
||||
|
||||
fun configureMockMvcTesterWithDefaultSettings() {
|
||||
// tag::default-customizations[]
|
||||
val mockMvc =
|
||||
MockMvcTester.of(listOf(HotelController())) { builder: StandaloneMockMvcBuilder ->
|
||||
builder.defaultRequest<StandaloneMockMvcBuilder>(
|
||||
MockMvcRequestBuilders.get("/")
|
||||
.contextPath("/app").servletPath("/main")
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
).build()
|
||||
}
|
||||
// end::default-customizations[]
|
||||
mockMvc.toString() // avoid warning
|
||||
}
|
||||
|
||||
|
||||
class HotelController
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
import org.springframework.web.context.WebApplicationContext
|
||||
|
||||
// tag::snippet[]
|
||||
@SpringJUnitWebConfig(ApplicationWebConfiguration::class)
|
||||
class AccountControllerIntegrationTests(@Autowired wac: WebApplicationContext) {
|
||||
|
||||
private val mockMvc = MockMvcTester.from(wac)
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
// end::snippet[]
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup
|
||||
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
|
||||
// tag::snippet[]
|
||||
class AccountControllerStandaloneTests {
|
||||
|
||||
val mockMvc = MockMvcTester.of(AccountController())
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
// end::snippet[]
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2002-2024 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup.converter
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.docs.testing.mockmvc.assertj.mockmvctestersetup.ApplicationWebConfiguration
|
||||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
import org.springframework.web.context.WebApplicationContext
|
||||
|
||||
// tag::snippet[]
|
||||
@SpringJUnitWebConfig(ApplicationWebConfiguration::class)
|
||||
class AccountControllerIntegrationTests(@Autowired wac: WebApplicationContext) {
|
||||
|
||||
private val mockMvc = MockMvcTester.from(wac).withHttpMessageConverters(
|
||||
listOf(wac.getBean(AbstractJackson2HttpMessageConverter::class.java)))
|
||||
|
||||
// ...
|
||||
|
||||
}
|
||||
// end::snippet[]
|
||||
|
||||
Reference in New Issue
Block a user