玩转springboot之SpringBoot单元测试

SpringBoot单元测试

spring单元测试

之前在spring项目中使用单元测试时是使用注解@RunWith(SpringJUnit4ClassRunner.class)来进行的

@RunWith(SpringJUnit4ClassRunner.class)// 通过自动织入从应用程序上下文向测试本身注入bean
@WebAppConfiguration // 指定web环境
@ContextConfiguration(locations 
= { // 指定配置文件
        "classpath*:springmvc.xml"
})

使用@WebAppConfiguration注解之后还可以注入WebApplicationContext环境

@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Before
public void setup(){
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
MockMvc

我们可以使用MockMvc来进行模拟请求

@Test
public void test() throws Exception {
    MockHttpServletResponse response = mockMvc.perform(MockMvcRequestBuilders.get("/json/testJson"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andReturn().getResponse();
    System.out.println(response.getContentAsString());

}
web安全测试

我们项目中经常会使用spring-security来进行权限,这就给我们的测试带来了麻烦,可以使用spring-security-test依赖来进行测试

<dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <version>5.1.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

在进行开启支持springSecurity

@Before
public void setup(){
    mockMvc = MockMvcBuilders
            .webAppContextSetup(webApplicationContext)
            .apply(SecurityMockMvcConfigurers.springSecurity())
            .build();
}

在写单元测试方法时,可以使用@WithMockUser来设置用户

@Test
@WithMockUser(username = "root",password = "123456",roles = "ADMIN")
public void testSecurity() throws Exception {
    MockHttpServletResponse response = mockMvc.perform(MockMvcRequestBuilders.get("/json/testJson"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andReturn().getResponse();
    System.out.println(response.getContentAsString());

}

然后使用测试的UserDetails来进行用户验证@WithUserDetails("root")

springboot单元测试

springboot中可以使用@SpringBootTest来进行单元测试,其中设置webEnvironment可以来定义运行模式,并在测试用例上使用@RunWith(SpringRunner.class)注解

enum WebEnvironment {

   // 加载WebApplicationContext,并提供一个mock servlet环境,使用该模式内嵌的servlet容器不会启动
   MOCK(false),

   // 加载EmbeddedWebApplicationContext,并提供一个真实的servlet环境,内嵌servlet容器启动,并监听一个随机端口
   RANDOM_PORT(true),

   // 加载EmbeddedWebApplicationContext,并提供一个真实的servlet环境,内嵌servlet容器启动,并监听一个定义好的接口
   DEFINED_PORT(true),

  // 使用SpringApplication加载一个ApplicationContext,但不提供servlet环境
   NONE(false);

}

示例

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests 
{

   @Autowired
   private CustomConfig config;

   @Test
   public void testProfile() {
      System.out.println(config.getName());
   }

}

https://zhhll.icu/2022/框架/springboot/基础/17.springboot单元测试/

本文由 mdnice 多平台发布

相关推荐

  1. springbootSpringBoot单元测试

    2024-07-11 11:22:04       21 阅读
  2. springbootSpringApplicationRunListener

    2024-07-11 11:22:04       21 阅读
  3. springbootspringboot项目监测

    2024-07-11 11:22:04       24 阅读
  4. springbootSpringBoot使用jsp

    2024-07-11 11:22:04       18 阅读
  5. springbootspringboot启动原理

    2024-07-11 11:22:04       19 阅读
  6. SpringBoot单元测试测试Service方法

    2024-07-11 11:22:04       20 阅读
  7. springbootxxxRunner接口使用

    2024-07-11 11:22:04       22 阅读
  8. springbootspringboot定制化tomcat

    2024-07-11 11:22:04       28 阅读
  9. 精通SpringBoot单元测试

    2024-07-11 11:22:04       40 阅读
  10. springboot 单元测试

    2024-07-11 11:22:04       37 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 11:22:04       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 11:22:04       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 11:22:04       45 阅读
  4. Python语言-面向对象

    2024-07-11 11:22:04       55 阅读

热门阅读

  1. 使用 Nuxt 3 搭建国际官网

    2024-07-11 11:22:04       17 阅读
  2. kafka-3

    kafka-3

    2024-07-11 11:22:04      15 阅读
  3. 华为机试HJ84统计大写字母个数

    2024-07-11 11:22:04       17 阅读
  4. MySQL中in和exists的区别

    2024-07-11 11:22:04       17 阅读
  5. Spring Boot 常用 Starter

    2024-07-11 11:22:04       19 阅读
  6. dify/api/models/tool.py文件中的数据表

    2024-07-11 11:22:04       17 阅读
  7. 【SQL】InnoDB的意向锁

    2024-07-11 11:22:04       21 阅读
  8. SpringSecurity中文文档(Servlet OAuth 2.0 Client)

    2024-07-11 11:22:04       16 阅读
  9. Linux串口设备的使用<ubuntu>

    2024-07-11 11:22:04       19 阅读
  10. 计算机如何学习

    2024-07-11 11:22:04       16 阅读