axzo-framework/axzo-test-spring-boot-starter
2022-08-15 16:59:01 +08:00
..
src 完善Demo示例 2022-07-07 18:08:49 +08:00
pom.xml anxzo-common rename 2022-08-15 16:59:01 +08:00
README.md 完善README,增加单元测试示例用法 2022-07-07 17:28:31 +08:00

axzo-test-spring-boot-starter

单元测试支持库. 更多细节用法请参考:

Quickstart

1、引入依赖

<!-- 单元测试依赖 -->
<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单元测试实例


@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请求

@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请求

@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