# 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 @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 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 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