完善README,增加单元测试示例用法

This commit is contained in:
zhourui 周锐 2022-07-07 17:28:31 +08:00
parent 88c8a9b1dd
commit 4a578b5fae
2 changed files with 102 additions and 8 deletions

View File

@ -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
<!-- 单元测试依赖 -->
<dependency>
<groupId>cn.axzo.framework</groupId>
<artifactId>axzo-test-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
```
### 2、Service单元测试实例
``` java
## Service测试 @SpringBootApplication(scanBasePackages = "cn.axzo")
@EnableFeignClients(basePackages = "cn.axzo")
public class WorkspaceServiceTest extends ServiceTestSupport {
@Autowired
private WorkspaceService service;
## Dao测试 @MockBean
XxlJobConfig jobConfig;
@Test
void testQueryWorkspace() {
WorkspaceQueryReq query = WorkspaceQueryReq.enptyQuery();
IPage<Workspace> 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;
## Controller @MockBean
XxlJobConfig jobConfig;
@Override
protected Object getTestController() {
return api;
}
## Http Rest 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();
}
}
```
#### 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<String,String> params = new HashMap<>();
//设置请求参数
params.put("workspaceId", String.valueOf(23));
String url = "/api/workspace/participatingUnit/listByWorkspace";
doGetAction(url, params)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
}
}
```
### 4、编写Dao测试用例
TODO

View File

@ -56,7 +56,8 @@
<dependency> <dependency>
<groupId>com.wix</groupId> <groupId>com.wix</groupId>
<artifactId>wix-embedded-mysql</artifactId> <artifactId>wix-embedded-mysql</artifactId>
<version>4.6.1</version> <version>4.6.2</version>
<optional>true</optional>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>