Migrate MockMvc tests to MockMvcTester
See gh-41198
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -21,20 +21,20 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@WebMvcTest(UserController.class)
|
||||
class MySecurityTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
private MockMvcTester mvc;
|
||||
|
||||
@Test
|
||||
@WithMockUser(roles = "ADMIN")
|
||||
void requestProtectedUrlWithUser() throws Exception {
|
||||
this.mvc.perform(get("/"));
|
||||
void requestProtectedUrlWithUser() {
|
||||
assertThat(this.mvc.get().uri("/")).doesNotHaveFailed();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,24 +22,22 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@WebMvcTest(UserController.class)
|
||||
@AutoConfigureRestDocs
|
||||
class MyUserDocumentationTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
private MockMvcTester mvc;
|
||||
|
||||
@Test
|
||||
void listUsers() throws Exception {
|
||||
this.mvc.perform(get("/users").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(document("list-users"));
|
||||
void listUsers() {
|
||||
assertThat(this.mvc.get().uri("/users").accept(MediaType.TEXT_PLAIN)).hasStatusOk()
|
||||
.apply(document("list-users"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -16,25 +16,23 @@
|
||||
|
||||
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringrestdocs.withmockmvc
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
|
||||
@WebMvcTest(UserController::class)
|
||||
@AutoConfigureRestDocs
|
||||
class MyUserDocumentationTests(@Autowired val mvc: MockMvc) {
|
||||
class MyUserDocumentationTests(@Autowired val mvc: MockMvcTester) {
|
||||
|
||||
@Test
|
||||
fun listUsers() {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/users").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk)
|
||||
.andDo(MockMvcRestDocumentation.document("list-users"))
|
||||
assertThat(mvc.get().uri("/users").accept(MediaType.TEXT_PLAIN))
|
||||
.hasStatusOk().apply(MockMvcRestDocumentation.document("list-users"))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -16,18 +16,17 @@
|
||||
|
||||
package org.springframework.boot.docs.features.testing.springbootapplications.springmvctests
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.mockito.BDDMockito.given
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||
import org.springframework.boot.test.mock.mockito.MockBean
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
|
||||
@WebMvcTest(UserVehicleController::class)
|
||||
class MyControllerTests(@Autowired val mvc: MockMvc) {
|
||||
class MyControllerTests(@Autowired val mvc: MockMvcTester) {
|
||||
|
||||
@MockBean
|
||||
lateinit var userVehicleService: UserVehicleService
|
||||
@@ -35,10 +34,9 @@ class MyControllerTests(@Autowired val mvc: MockMvc) {
|
||||
@Test
|
||||
fun testExample() {
|
||||
given(userVehicleService.getVehicleDetails("sboot"))
|
||||
.willReturn(VehicleDetails("Honda", "Civic"))
|
||||
mvc.perform(MockMvcRequestBuilders.get("/sboot/vehicle").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk)
|
||||
.andExpect(MockMvcResultMatchers.content().string("Honda Civic"))
|
||||
.willReturn(VehicleDetails("Honda", "Civic"))
|
||||
assertThat(mvc.get().uri("/sboot/vehicle").accept(MediaType.TEXT_PLAIN))
|
||||
.hasStatusOk().hasBodyTextEqualTo("Honda Civic")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -16,24 +16,23 @@
|
||||
|
||||
package org.springframework.boot.docs.features.testing.springbootapplications.withmockenvironment
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.test.web.reactive.server.WebTestClient
|
||||
import org.springframework.test.web.reactive.server.expectBody
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
|
||||
@SpringBootTest
|
||||
@AutoConfigureMockMvc
|
||||
class MyMockMvcTests {
|
||||
|
||||
@Test
|
||||
fun testWithMockMvc(@Autowired mvc: MockMvc) {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isOk)
|
||||
.andExpect(MockMvcResultMatchers.content().string("Hello World"))
|
||||
fun testWithMockMvc(@Autowired mvc: MockMvcTester) {
|
||||
assertThat(mvc.get().uri("/")).hasStatusOk()
|
||||
.hasBodyTextEqualTo("Hello World")
|
||||
}
|
||||
|
||||
// If Spring WebFlux is on the classpath, you can drive MVC tests with a WebTestClient
|
||||
@@ -41,10 +40,10 @@ class MyMockMvcTests {
|
||||
@Test
|
||||
fun testWithWebTestClient(@Autowired webClient: WebTestClient) {
|
||||
webClient
|
||||
.get().uri("/")
|
||||
.exchange()
|
||||
.expectStatus().isOk
|
||||
.expectBody<String>().isEqualTo("Hello World")
|
||||
.get().uri("/")
|
||||
.exchange()
|
||||
.expectStatus().isOk
|
||||
.expectBody<String>().isEqualTo("Hello World")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2022 the original author or authors.
|
||||
* Copyright 2012-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.
|
||||
@@ -16,20 +16,21 @@
|
||||
|
||||
package org.springframework.boot.docs.howto.testing.withspringsecurity
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
|
||||
import org.springframework.security.test.context.support.WithMockUser
|
||||
import org.springframework.test.web.servlet.MockMvc
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||
import org.springframework.test.web.servlet.assertj.MockMvcTester
|
||||
|
||||
@WebMvcTest(UserController::class)
|
||||
class MySecurityTests(@Autowired val mvc: MockMvc) {
|
||||
class MySecurityTests(@Autowired val mvc: MockMvcTester) {
|
||||
|
||||
@Test
|
||||
@WithMockUser(roles = ["ADMIN"])
|
||||
fun requestProtectedUrlWithUser() {
|
||||
mvc.perform(MockMvcRequestBuilders.get("/"))
|
||||
assertThat(mvc.get().uri("/"))
|
||||
.doesNotHaveFailed()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user