Polish MockMvcReuseTests

This commit is contained in:
Sam Brannen 2020-12-28 16:03:16 +01:00
parent 885f6dbab9
commit ee41ebc1ab
1 changed files with 19 additions and 26 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,17 +16,14 @@
package org.springframework.test.web.servlet; package org.springframework.test.web.servlet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ -47,29 +44,25 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppC
* @author Rob Winch * @author Rob Winch
* @since 4.2 * @since 4.2
*/ */
@ExtendWith(SpringExtension.class) @SpringJUnitWebConfig
@ContextConfiguration @TestInstance(Lifecycle.PER_CLASS)
@WebAppConfiguration class MockMvcReuseTests {
public class MockMvcReuseTests {
private static final String HELLO = "hello"; private static final String HELLO = "hello";
private static final String ENIGMA = "enigma"; private static final String ENIGMA = "enigma";
private static final String FOO = "foo"; private static final String FOO = "foo";
private static final String BAR = "bar"; private static final String BAR = "bar";
@Autowired private final MockMvc mvc;
private WebApplicationContext wac;
private MockMvc mvc;
@BeforeEach MockMvcReuseTests(WebApplicationContext wac) {
public void setUp() { this.mvc = webAppContextSetup(wac).build();
this.mvc = webAppContextSetup(this.wac).build();
} }
@Test @Test
public void sessionAttributesAreClearedBetweenInvocations() throws Exception { void sessionAttributesAreClearedBetweenInvocations() throws Exception {
this.mvc.perform(get("/")) this.mvc.perform(get("/"))
.andExpect(content().string(HELLO)) .andExpect(content().string(HELLO))
@ -85,7 +78,7 @@ public class MockMvcReuseTests {
} }
@Test @Test
public void requestParametersAreClearedBetweenInvocations() throws Exception { void requestParametersAreClearedBetweenInvocations() throws Exception {
this.mvc.perform(get("/")) this.mvc.perform(get("/"))
.andExpect(content().string(HELLO)); .andExpect(content().string(HELLO));
@ -102,7 +95,7 @@ public class MockMvcReuseTests {
static class Config { static class Config {
@Bean @Bean
public MyController myController() { MyController myController() {
return new MyController(); return new MyController();
} }
} }
@ -110,13 +103,13 @@ public class MockMvcReuseTests {
@RestController @RestController
static class MyController { static class MyController {
@RequestMapping("/") @GetMapping("/")
public String hello() { String hello() {
return HELLO; return HELLO;
} }
@RequestMapping(path = "/", params = ENIGMA) @GetMapping(path = "/", params = ENIGMA)
public String enigma() { String enigma() {
return ENIGMA; return ENIGMA;
} }
} }