From 4a578b5fae7a70e667634719fb7c37fbfbf43c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?zhourui=20=E5=91=A8=E9=94=90?= Date: Thu, 7 Jul 2022 17:28:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84README=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E7=A4=BA=E4=BE=8B?= =?UTF-8?q?=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- axzo-test-spring-boot-starter/README.md | 107 ++++++++++++++++++++++-- axzo-test-spring-boot-starter/pom.xml | 3 +- 2 files changed, 102 insertions(+), 8 deletions(-) diff --git a/axzo-test-spring-boot-starter/README.md b/axzo-test-spring-boot-starter/README.md index 9aef46f..4e86f1c 100644 --- a/axzo-test-spring-boot-starter/README.md +++ b/axzo-test-spring-boot-starter/README.md @@ -1,18 +1,111 @@ -## axzo-test-spring-boot-starter +# axzo-test-spring-boot-starter -单元测试支持库 +单元测试支持库. +更多细节用法请参考: +- [Spring Testing](https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#testing) -## 使用方法 +- [Spring Boot Testing](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing) +## Quickstart +### 1、引入依赖: +``` xml + + + cn.axzo.framework + axzo-test-spring-boot-starter + 1.0.0-SNAPSHOT + test + +``` +### 2、Service单元测试实例 +``` java -## Service测试 +@SpringBootApplication(scanBasePackages = "cn.axzo") +@EnableFeignClients(basePackages = "cn.axzo") +public class WorkspaceServiceTest extends ServiceTestSupport { + + @Autowired + private WorkspaceService service; + @MockBean + XxlJobConfig jobConfig; -## Dao测试 + @Test + void testQueryWorkspace() { + WorkspaceQueryReq query = WorkspaceQueryReq.enptyQuery(); + IPage page = service.queryWorkspace(query); + page.getRecords().iterator().forEachRemaining( + workspace -> System.out.println(workspace) + ); + } +} +``` +> @MockBean注解使用: +``` +Mocks can be registered by type or by bean name. Any existing single bean of the same type defined in the context will be replaced by the mock. If no existing bean is defined a new one will be added. +``` +### 3、Controller测试示例 +#### 3.1、 Post请求 +``` java +@SpringBootApplication(scanBasePackages = "cn.axzo") +@EnableFeignClients(basePackages = "cn.axzo") +class WorkspaceApiImplTest extends ControllerTestSupport { + @Autowired + WorkspaceApiImpl api; + @MockBean + XxlJobConfig jobConfig; + + @Override + protected Object getTestController() { + return api; + } + + @Test + void testQueryWorkspace() throws Exception { + String url = "/api/workspace/queryWorkspace"; + //设置请求参数 + WorkspaceQueryReq query = WorkspaceQueryReq.enptyQuery(); + query.setType(2); + + doPostAction(url, query) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + } -## Controller +} +``` +#### 3.2 Get请求 +``` java +@SpringBootApplication(scanBasePackages = "cn.axzo") +@EnableFeignClients(basePackages = "cn.axzo") +class ParticipatingUnitApiImplTest extends ControllerTestSupport { + @Autowired + ParticipatingUnitApiImpl api; + + @Override + protected Object getTestController() { + return api; + } + + @MockBean + XxlJobConfig jobConfig; + + @Test + void testListByWorkspace() throws Exception { + Map params = new HashMap<>(); + //设置请求参数 + params.put("workspaceId", String.valueOf(23)); -## Http Rest Api测试 + String url = "/api/workspace/participatingUnit/listByWorkspace"; + doGetAction(url, params) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andReturn(); + } + +} +``` +### 4、编写Dao测试用例 +TODO \ No newline at end of file diff --git a/axzo-test-spring-boot-starter/pom.xml b/axzo-test-spring-boot-starter/pom.xml index 831e183..0ced69a 100644 --- a/axzo-test-spring-boot-starter/pom.xml +++ b/axzo-test-spring-boot-starter/pom.xml @@ -56,7 +56,8 @@ com.wix wix-embedded-mysql - 4.6.1 + 4.6.2 + true org.apache.commons