Spring Boot & 单元测试

本文主要介绍Spring Boot如何完成各种不同类型的单元测试

Spring Boot入门系列

  • Spring Boot七分钟快速实践
  • Spring Boot & MyBatis
  • Spring Boot & Redis
  • Spring Boot & Swagger
  • Spring Boot & 单元测试
  • Spring Boot & Actuator
  • Spring Boot Admin

Spring基本单元测试

  • pom.xml

    org.springframework.boot
    spring-boot-starter-test
    test

  • 测试代码
@RunWith(SpringRunner.class)
//启动Spring
@SpringBootTest
public class HelloControllerTest {

    @Autowired
    private HelloController helloController;

    @Test
    public void getHello() throws Exception {
        final String greeting = helloController.index();
        Assert.assertEquals("Hello World!", greeting);
    }
}

Mock的方式进行Spring MVC单元测试

@RunWith(SpringRunner.class)
@SpringBootTest
//配置mock
@AutoConfigureMockMvc
public class HelloControllerTest {

    @Autowired
    private MockMvc mvc;

    @Test
    public void getHello() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("Hello World!")));
    }
}

容器的方式启动进行Spring MVC单元测试

  • pom.xml

    org.springframework.boot
    spring-boot-starter-webflux
    test

  • 配置服务器与客户端
@RunWith(SpringRunner.class)
//配置本地随机端口,服务器会选择一个空闲的端口使用,避免端口冲突
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerServerTest {
    @Autowired
    private WebTestClient webClient;

    @Test
    public void exampleTest() {
        this.webClient.get().uri("/").exchange().expectStatus().isOk()
                .expectBody(String.class).isEqualTo("Hello World!");
    }
}

使用Spock完成单元测试

  • pom.xml

    org.spockframework
    spock-spring
    1.1-groovy-2.4


    org.codehaus.groovy
    groovy-all
    2.4.16





    org.codehaus.gmavenplus
    gmavenplus-plugin
    1.6.1
    
        
            
                compile
                compileTests
            
        
    

  • 测试代码
import static org.hamcrest.Matchers.equalTo
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

//注意这里不能使用@RunWith(SpringRunner.class)了
@SpringBootTest
@AutoConfigureMockMvc
class HelloControllerTest extends Specification {
    @Autowired
    private MockMvc mvc

    def "mvc"() {
        expect:
        mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("Hello World!")));
    }
}

常见错误

  • Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

@SpringBootTest搜索@SpringBootApplication注解的类,是按照test所在类的package往父层级逐级搜索。 所以解决办法一:@SpringBootTest(classes = Application.class),解决方案二:修改test所在类的package,放到@SpringBootApplication子package

参考

  • https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html
  • 聊聊 Spring Boot 2.0 的 WebFlux
  • Error: Unable to find @SpringBootConfiguration when doing @WebMvcTest for Spring Controller

你可能感兴趣的:(Spring Boot & 单元测试)