原创

springboot 单元测试

1、pom文件中,引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

2、src/test/java目录下,新建Test.java

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {

    @Autowired
    ......

    @org.junit.Test
    public void test(){

        // 单元测试内容

    }

}

注意:
如果项目中用到了 WebSocket
需要配置 webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
随机端口启动

正文到此结束