简体   繁体   中英

How apply different configurations per method in Spring Boot test class?

I have an annotation that pulls in some configurations (via @Import ). I want to test running with and without that annotation. I want to do this in one test class.

I know that I can change the spring context per method @DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD) , but I don't know how to have it run with or without the annotation on different methods.

How would I achieve this?

What I want to do:

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();
    }
}

Using nested classes, you could do something like this:

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
    }
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM