SpringBoot项目使用Junit进行单元测试

发布时间 2023-04-25 22:36:12作者: zolmk

SpringBoot项目使用Junit进行单元测试

环境:Springboot 2.6.7

POM.xml文件

添加如下内容:

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

单元测试类


import reactor.core.publisher.Mono;

import static net.bytebuddy.matcher.ElementMatchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class UserServiceTest {
    @Autowired
    private UserService userService;
    @Test
    public void test() throws Exception{
    }

    private String getToken() {
        return "";
    }

    @Test
    public void testService() throws Exception {
        Object o = userService.listUsers(0, 10);
        System.out.println(JSONUtil.toJsonStr((((Mono)o).block())));
    }
}