111 lines
2.7 KiB
Markdown
111 lines
2.7 KiB
Markdown
# 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
|
||
|
||
@SpringBootApplication(scanBasePackages = "cn.axzo")
|
||
@EnableFeignClients(basePackages = "cn.axzo")
|
||
public class WorkspaceServiceTest extends ServiceTestSupport {
|
||
|
||
@Autowired
|
||
private WorkspaceService service;
|
||
|
||
@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;
|
||
|
||
@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();
|
||
}
|
||
|
||
}
|
||
```
|
||
#### 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 |