From bd92fd202b10a9ddd47fc80aec50fb4c79599dc0 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 20 Jul 2019 14:51:32 +0200 Subject: [PATCH] Playground for AssertJ support in MockMvc See gh-21178 --- build.gradle | 4 +- settings.gradle | 1 + spring-test-test/spring-test-test.gradle | 15 +++++ .../test/context/web/MockMvcTests.java | 66 +++++++++++++++++++ .../test/context/web/Person.java | 15 +++++ .../test/context/web/PersonController.java | 19 ++++++ .../test/context/web/WebConfig.java | 16 +++++ .../src/test/resources/log4j2-test.xml | 36 ++++++++++ spring-test/spring-test.gradle | 1 + .../test/web/servlet/MockMvc.java | 17 +++++ .../test/web/servlet/MvcFluent.java | 29 ++++++++ .../test/web/servlet/ResultActions.java | 2 + 12 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 spring-test-test/spring-test-test.gradle create mode 100644 spring-test-test/src/test/java/org/springframework/test/context/web/MockMvcTests.java create mode 100644 spring-test-test/src/test/java/org/springframework/test/context/web/Person.java create mode 100644 spring-test-test/src/test/java/org/springframework/test/context/web/PersonController.java create mode 100644 spring-test-test/src/test/java/org/springframework/test/context/web/WebConfig.java create mode 100644 spring-test-test/src/test/resources/log4j2-test.xml create mode 100644 spring-test/src/main/java/org/springframework/test/web/servlet/MvcFluent.java diff --git a/build.gradle b/build.gradle index 3ea138eb7d9e..eb6568aeedd2 100644 --- a/build.gradle +++ b/build.gradle @@ -169,7 +169,9 @@ configure(allprojects) { project -> } testCompile("io.mockk:mockk:1.9.3") testCompile("org.hamcrest:hamcrest-all:1.3") - testCompile("org.assertj:assertj-core:3.12.2") + if (!project.name.equals("spring-test-test")) { + testCompile("org.assertj:assertj-core:3.12.2") + } // Pull in the latest JUnit 5 Launcher API and the Vintage engine as well // so that we can run JUnit 4 tests in IDEs. testRuntime("org.junit.platform:junit-platform-launcher") diff --git a/settings.gradle b/settings.gradle index 907d85a759c7..5871d9d8e014 100644 --- a/settings.gradle +++ b/settings.gradle @@ -15,6 +15,7 @@ include "spring-messaging" include "spring-orm" include "spring-oxm" include "spring-test" +include "spring-test-test" include "spring-tx" include "spring-web" include "spring-webmvc" diff --git a/spring-test-test/spring-test-test.gradle b/spring-test-test/spring-test-test.gradle new file mode 100644 index 000000000000..7d71885393ef --- /dev/null +++ b/spring-test-test/spring-test-test.gradle @@ -0,0 +1,15 @@ +description = "Tests for spring-test" + +dependencies { + testCompile(project(":spring-webmvc")) + testCompile("javax.servlet:javax.servlet-api:4.0.1") + testCompile(project(":spring-test")) { + exclude group: "org.assertj", module: "assertj-core" + } + testCompile("junit:junit:4.13-beta-3") { + exclude group: "org.hamcrest", module: "hamcrest-core" + } + testCompile("org.hamcrest:hamcrest-all:1.3") + testCompile("com.jayway.jsonpath:json-path:2.4.0") + testCompile("com.fasterxml.jackson.core:jackson-databind:${jackson2Version}") +} diff --git a/spring-test-test/src/test/java/org/springframework/test/context/web/MockMvcTests.java b/spring-test-test/src/test/java/org/springframework/test/context/web/MockMvcTests.java new file mode 100644 index 000000000000..357734adeb42 --- /dev/null +++ b/spring-test-test/src/test/java/org/springframework/test/context/web/MockMvcTests.java @@ -0,0 +1,66 @@ + +package org.springframework.test.context.web; + +import java.net.URI; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.context.WebApplicationContext; + +import static org.hamcrest.Matchers.is; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = WebConfig.class) +@WebAppConfiguration +public class MockMvcTests { + + @Autowired + WebApplicationContext wac; + + MockMvc mockMvc; + + @Before + public void setUpMockMvc() { + this.mockMvc = webAppContextSetup(wac)// + // .alwaysDo(print(System.err))// + .alwaysExpect(status().isOk())// + .alwaysExpect(content().contentTypeCompatibleWith(APPLICATION_JSON))// + .build(); + } + + @Test + public void getPerson42() throws Exception { + this.mockMvc.perform(get("/person/42").accept(MediaType.APPLICATION_JSON))// + .andExpect(jsonPath("$.name", is("Dilbert"))); + } + + @Test + public void fluentAndReturn() throws Exception { + this.mockMvc.perform(get("/person/42").accept(MediaType.APPLICATION_JSON))// + .fluent()// + .andReturn(); + } + + @Test + public void fluentAndAssertThat() throws Exception { + this.mockMvc.perform(get("/person/42").accept(MediaType.APPLICATION_JSON))// + .fluent() + // .assertThat(new URI("https://spring.io").isAbsolute(); + ; + } + +} diff --git a/spring-test-test/src/test/java/org/springframework/test/context/web/Person.java b/spring-test-test/src/test/java/org/springframework/test/context/web/Person.java new file mode 100644 index 000000000000..cb2f91db2b41 --- /dev/null +++ b/spring-test-test/src/test/java/org/springframework/test/context/web/Person.java @@ -0,0 +1,15 @@ +package org.springframework.test.context.web; + +public class Person { + + private final String name; + + Person(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } + +} diff --git a/spring-test-test/src/test/java/org/springframework/test/context/web/PersonController.java b/spring-test-test/src/test/java/org/springframework/test/context/web/PersonController.java new file mode 100644 index 000000000000..80c51b366f49 --- /dev/null +++ b/spring-test-test/src/test/java/org/springframework/test/context/web/PersonController.java @@ -0,0 +1,19 @@ +package org.springframework.test.context.web; + +import org.springframework.test.context.junit.jupiter.comics.Person; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController +class PersonController { + + @GetMapping("/person/{id}") + Person getPerson(@PathVariable long id) { + if (id == 42) { + return new Person("Dilbert"); + } + return new Person("Wally"); + } + +} diff --git a/spring-test-test/src/test/java/org/springframework/test/context/web/WebConfig.java b/spring-test-test/src/test/java/org/springframework/test/context/web/WebConfig.java new file mode 100644 index 000000000000..e90d5aa612db --- /dev/null +++ b/spring-test-test/src/test/java/org/springframework/test/context/web/WebConfig.java @@ -0,0 +1,16 @@ +package org.springframework.test.context.web; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; + +@Configuration +@EnableWebMvc +class WebConfig { + + @Bean + PersonController personController() { + return new PersonController(); + } + +} diff --git a/spring-test-test/src/test/resources/log4j2-test.xml b/spring-test-test/src/test/resources/log4j2-test.xml new file mode 100644 index 000000000000..db28ecb3db60 --- /dev/null +++ b/spring-test-test/src/test/resources/log4j2-test.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-test/spring-test.gradle b/spring-test/spring-test.gradle index 8bac3ee1e9ee..b6102742be0f 100644 --- a/spring-test/spring-test.gradle +++ b/spring-test/spring-test.gradle @@ -34,6 +34,7 @@ dependencies { optional("org.aspectj:aspectjweaver:${aspectjVersion}") optional("org.codehaus.groovy:groovy:${groovyVersion}") optional("org.hamcrest:hamcrest-core:1.3") + optional("org.assertj:assertj-core:3.12.2") optional("org.apache.taglibs:taglibs-standard-jstlel:1.2.5") { exclude group: "org.apache.taglibs", module: "taglibs-standard-spec" } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/MockMvc.java b/spring-test/src/main/java/org/springframework/test/web/servlet/MockMvc.java index 69c0ea41c700..1ceb28ad758f 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/MockMvc.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/MockMvc.java @@ -16,6 +16,7 @@ package org.springframework.test.web.servlet; +import java.net.URI; import java.util.ArrayList; import java.util.List; import javax.servlet.AsyncContext; @@ -26,6 +27,8 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; +import org.assertj.core.api.UriAssert; + import org.springframework.beans.Mergeable; import org.springframework.lang.Nullable; import org.springframework.mock.web.MockFilterChain; @@ -204,6 +207,20 @@ public ResultActions andDo(ResultHandler handler) throws Exception { public MvcResult andReturn() { return mvcResult; } + @Override + public MvcFluent fluent() { + return new MvcFluent() { + @Override + public UriAssert assertThat(URI actual) { + return new UriAssert(actual); + } + + @Override + public MvcResult andReturn() { + return mvcResult; + } + }; + } }; } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/MvcFluent.java b/spring-test/src/main/java/org/springframework/test/web/servlet/MvcFluent.java new file mode 100644 index 000000000000..2715182f3fa4 --- /dev/null +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/MvcFluent.java @@ -0,0 +1,29 @@ +/* + * Copyright 2002-2019 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 + * + * http://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.test.web.servlet; + +import java.net.URI; + +import org.assertj.core.api.UriAssert; + +public interface MvcFluent { + + UriAssert assertThat(URI actual); + + MvcResult andReturn(); + +} diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/ResultActions.java b/spring-test/src/main/java/org/springframework/test/web/servlet/ResultActions.java index d63a5dd4884e..08f845c797d0 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/ResultActions.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/ResultActions.java @@ -78,4 +78,6 @@ public interface ResultActions { */ MvcResult andReturn(); + MvcFluent fluent(); + }