Skip to content

Commit

Permalink
Playground for AssertJ support in MockMvc
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jul 20, 2019
1 parent be65bef commit bd92fd2
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 15 additions & 0 deletions spring-test-test/spring-test-test.gradle
Original file line number Diff line number Diff line change
@@ -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}")
}
Original file line number Diff line number Diff line change
@@ -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();
;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
36 changes: 36 additions & 0 deletions spring-test-test/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{1.} - %msg%n" />
</Console>
<File name="File" fileName="build/logs/spring-test.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{1.} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Logger name="org.junit.platform" level="info" />
<Logger name="org.springframework.test.context" level="warn" />
<Logger name="org.springframework.test.context.TestContext" level="warn" />
<Logger name="org.springframework.test.context.TestContextManager" level="warn" />
<Logger name="org.springframework.test.context.ContextLoaderUtils" level="warn" />
<Logger name="org.springframework.test.context.cache" level="warn" />
<Logger name="org.springframework.test.context.junit4.rules" level="warn" />
<Logger name="org.springframework.test.context.transaction.TransactionalTestExecutionListener" level="warn" />
<Logger name="org.springframework.test.context.web" level="warn" />
<!-- The following must be kept at DEBUG in order to test SPR-14363. -->
<Logger name="org.springframework.test.util" level="debug" />
<!--
<Logger name="org.springframework.test.context.support" level="info" />
<Logger name="org.springframework.test.context.support.DelegatingSmartContextLoader" level="info" />
<Logger name="org.springframework.test.context.support.AbstractGenericContextLoader" level="info" />
<Logger name="org.springframework.test.context.support.AnnotationConfigContextLoader" level="info" />
<Logger name="org.springframework.beans" level="warn" />
<Logger name="org.springframework.test.web.servlet.result" level="debug" />
-->
<Root level="error">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
1 change: 1 addition & 0 deletions spring-test/spring-test.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
};
}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@ public interface ResultActions {
*/
MvcResult andReturn();

MvcFluent fluent();

}

0 comments on commit bd92fd2

Please sign in to comment.