资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot中怎么利用JUnit5实现单元测试

这篇文章给大家介绍Spring Boot中怎么利用JUnit 5实现单元测试,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联建站于2013年创立,是专业互联网技术服务公司,拥有项目网站设计制作、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元平陆做网站,已为上家服务,为平陆各地企业和个人服务,联系电话:13518219792

1. 忽略测试用例执行

JUnit 4:

@Test  @Ignore  public void testMethod() {     // ...  }

JUnit 5:

@Test  @Disabled("explanation")  public void testMethod() {     // ...  }

2. RunWith 配置

JUnit 4:

@RunWith(SpringRunner.class)  @SpringBootTest  public class ApplicationTests {      @Test      public void contextLoads() {      }  }

JUnit 5:

@ExtendWith(SpringExtension.class)  @SpringBootTest public class ApplicationTests {      @Test      public void contextLoads() {      }  }

3. @Before、@BeforeClass、@After、@AfterClass 被替换

  •  @BeforeEach 替换 @Before

  •  @BeforeAll 替换 @BeforeClass

  •  @AfterEach 替换 @After

  •  @AfterAll 替换 @AfterClass

开发环境

  •  JDK 8

示例

1.创建 Spring Boot 工程。

2.添加 spring-boot-starter-web 依赖,最终 pom.xml 如下。

        4.0.0                org.springframework.boot          spring-boot-starter-parent          2.2.6.RELEASE                      tutorial.spring.boot      spring-boot-junit5      0.0.1-SNAPSHOT      spring-boot-junit5      Demo project for Spring Boot Unit Test with JUnit 5                1.8                                    org.springframework.boot              spring-boot-starter-web                                  org.springframework.boot              spring-boot-starter-test              test                                                      org.junit.vintage                      junit-vintage-engine                                                                                                org.springframework.boot                  spring-boot-maven-plugin                               

3.工程创建好之后自动生成了一个测试类。

package tutorial.spring.boot.junit5;  import org.junit.jupiter.api.Test;  import org.springframework.boot.test.context.SpringBootTest;  @SpringBootTest  class SpringBootJunit5ApplicationTests {      @Test      void contextLoads() {      }  }

这个测试类的作用是检查应用程序上下文是否可正常启动。@SpringBootTest 注解告诉 Spring Boot 查找带 @SpringBootApplication 注解的主配置类,并使用该类启动 Spring 应用程序上下文。Java知音公众号内回复“后端面试”, 送你一份Java面试题宝典

4.补充待测试应用逻辑代码

4.1. 定义 Service 层接口

package tutorial.spring.boot.junit5.service;  public interface HelloService {     String hello(String name);  }

4.2. 定义 Controller 层

package tutorial.spring.boot.junit5.controller;  import org.springframework.web.bind.annotation.GetMapping;  import org.springframework.web.bind.annotation.PathVariable;  import org.springframework.web.bind.annotation.RestController;  import tutorial.spring.boot.junit5.service.HelloService;  @RestController  public class HelloController {      private final HelloService helloService;      public HelloController(HelloService helloService) {         this.helloService = helloService;      }      @GetMapping("/hello/{name}")      public String hello(@PathVariable("name") String name) {          return helloService.hello(name);      }  }

4.3. 定义 Service 层实现

package tutorial.spring.boot.junit5.service.impl;  import org.springframework.stereotype.Service;  import tutorial.spring.boot.junit5.service.HelloService;  @Service public class HelloServiceImpl implements HelloService {      @Override      public String hello(String name) {          return "Hello, " + name;      }  }

5.编写发送 HTTP 请求的单元测试。

package tutorial.spring.boot.junit5;  import org.assertj.core.api.Assertions;  import org.junit.jupiter.api.Test;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.boot.test.context.SpringBootTest;  import org.springframework.boot.test.web.client.TestRestTemplate;  import org.springframework.boot.web.server.LocalServerPort;  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)  public class HttpRequestTest {      @LocalServerPort      private int port;      @Autowired      private TestRestTemplate restTemplate;     @Test      public void testHello() {          String requestResult = this.restTemplate.getForObject("http://127.0.0.1:" + port + "/hello/spring",                  String.class);          Assertions.assertThat(requestResult).contains("Hello, spring");      }  }

说明:

  •  webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT 使用本地的一个随机端口启动服务;

  •  @LocalServerPort 相当于 @Value("${local.server.port}");

  •  在配置了 webEnvironment 后,Spring Boot 会自动提供一个 TestRestTemplate 实例,可用于发送 HTTP 请求。

  •  除了使用 TestRestTemplate 实例发送 HTTP 请求外,还可以借助 org.springframework.test.web.servlet.MockMvc 完成类似功能,代码如下: 

package tutorial.spring.boot.junit5.controller;  import org.assertj.core.api.Assertions;  import org.junit.jupiter.api.Test;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;  import org.springframework.boot.test.context.SpringBootTest;  import org.springframework.test.web.servlet.MockMvc;  import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;  import org.springframework.test.web.servlet.result.MockMvcResultHandlers;  import org.springframework.test.web.servlet.result.MockMvcResultMatchers;  @SpringBootTest  @AutoConfigureMockMvc  public class HelloControllerTest {      @Autowired      private HelloController helloController;      @Autowired      private MockMvc mockMvc;      @Test      public void testNotNull() {          Assertions.assertThat(helloController).isNotNull();      }      @Test      public void testHello() throws Exception {          this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))                  .andDo(MockMvcResultHandlers.print())                  .andExpect(MockMvcResultMatchers.status().isOk())                  .andExpect(MockMvcResultMatchers.content().string("Hello, spring"));     }  }

以上测试方法属于整体测试,即将应用上下文全都启动起来,还有一种分层测试方法,譬如仅测试 Controller 层。

6.分层测试。

package tutorial.spring.boot.junit5.controller;  import org.assertj.core.api.Assertions;  import org.junit.jupiter.api.Test;  import org.mockito.Mockito;  import org.springframework.beans.factory.annotation.Autowired;  import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;  import org.springframework.boot.test.mock.mockito.MockBean;  import org.springframework.test.web.servlet.MockMvc;  import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;  import org.springframework.test.web.servlet.result.MockMvcResultHandlers;  import org.springframework.test.web.servlet.result.MockMvcResultMatchers;  import tutorial.spring.boot.junit5.service.HelloService;  @WebMvcTest  public class HelloControllerTest {      @Autowired      private HelloController helloController;      @Autowired      private MockMvc mockMvc;     @MockBean      private HelloService helloService;      @Test      public void testNotNull() {          Assertions.assertThat(helloController).isNotNull();      }      @Test      public void testHello() throws Exception {          Mockito.when(helloService.hello(Mockito.anyString())).thenReturn("Mock hello");          this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))                  .andDo(MockMvcResultHandlers.print())                  .andExpect(MockMvcResultMatchers.status().isOk())                  .andExpect(MockMvcResultMatchers.content().string("Mock hello"));      }  }

说明:

@WebMvcTest 注释告诉 Spring Boot 仅实例化 Controller 层,而不去实例化整体上下文,还可以进一步指定仅实例化 Controller 层的某个实例:@WebMvcTest(HelloController.class);

因为只实例化了 Controller 层,所以依赖的 Service 层实例需要通过 @MockBean 创建,并通过 Mockito 的方法指定 Mock 出来的 Service 层实例在特定情况下方法调用时的返回结果。

关于Spring Boot中怎么利用JUnit 5实现单元测试就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


新闻名称:SpringBoot中怎么利用JUnit5实现单元测试
链接地址:http://cdkjz.cn/article/jijdss.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220