forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Playground for AssertJ support in MockMvc
- Loading branch information
Showing
12 changed files
with
220 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") | ||
} |
66 changes: 66 additions & 0 deletions
66
spring-test-test/src/test/java/org/springframework/test/context/web/MockMvcTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
; | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
spring-test-test/src/test/java/org/springframework/test/context/web/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
spring-test-test/src/test/java/org/springframework/test/context/web/PersonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
spring-test-test/src/test/java/org/springframework/test/context/web/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
spring-test/src/main/java/org/springframework/test/web/servlet/MvcFluent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,4 +78,6 @@ public interface ResultActions { | |
*/ | ||
MvcResult andReturn(); | ||
|
||
MvcFluent fluent(); | ||
|
||
} |