如何在Spring Boot测试类中应用每种方法的不同配置?

时间:2017-10-23 21:05:47

标签: java spring spring-mvc spring-boot mockmvc

我有一个注释可以引入一些配置(通过@Import)。我想测试带有和不带注释的运行。我想在一个测试类中执行此操作。

我知道我可以根据方法@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)更改弹簧上下文,但我不知道如何在不同方法的情况下使用或不使用注释运行它。

我将如何实现这一目标?

我想做什么:

package com.test.reference.cors;

import com.test.EnableCors;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(secure = false)
@ContextConfiguration(classes = {ControllerConfig.class, ReferenceController.class})
public class TestCORS
{
    private MockMvc mockMvc;
    private ObjectMapper objectMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void setup()
    {
        //Create an environment for it
        mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext)
                .dispatchOptions(true).build();

        //Create our marshaller
        objectMapper = new ObjectMapper();
    }

    @Test
    public void testWithoutCors() throws Exception
    {
        //Call to test a date
        MvcResult result = mockMvc.perform(
            options("/v1/testdate")
                .contentType(MediaType.APPLICATION_JSON)
                //CORS HEADERS
                .header("Access-Control-Request-Method", "DELETE")
                .header("Origin", "https://evil.com")
        ).andExpect(status().isForbidden())
                .andReturn();
    }

    @Test
    @EnableCors
    public void testWithCors() throws Exception
    {
        //Call to test a date
        MvcResult result = mockMvc.perform(
                options("/v1/testdate")
                        .contentType(MediaType.APPLICATION_JSON)
                        //CORS HEADERS
                        .header("Access-Control-Request-Method", "POST")
                        .header("Origin", "http://evil.com")
        ).andExpect(status().isOk())
                .andReturn();
    }
}

1 个答案:

答案 0 :(得分:1)

使用嵌套类,您可以执行以下操作:

class NewFeatureTest {


@SpringBootTest
protected static class NewFeatureWithDefaultConfigTest {

    @Autowired
    ApplicationContext context;

    @Test
    void yourTestMethod() {
        
    }
    
    @Configuration(proxyBeanMethods = false)
    @EnableAutoConfiguration
    protected static class Config {
        
        // your 1st config

    }
}

@SpringBootTest
protected static class NewFeatureWithDefaultsTests {

    @Autowired
    ApplicationContext context;

    @Test
    void yourOtherTestMethod() {
        
    }


    @Configuration(proxyBeanMethods = false)
    @EnableAutoConfiguration
    protected static class NoDefaultsConfig {
          // your 2nd config
    }
}}