首先创建SpringBoot工程,引入SpringCloud相关的maven依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-netflix-core -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-core</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
编写相关接口
public interface UamUserStatusApi {
@ApiOperation(value = "更新用户状态", tags = {"用户API"})
@ApiResponses(value = { @ApiResponse(code = 200, message = "成功响应") })
@RequestMapping(value = "/{userId}/status/update", produces = { "application/json" }, method = RequestMethod.GET)
ApiResult<?> updateUserStatus(@ApiParam(value = "用户id", required = true) @PathVariable(value = "userId") String userId,
@ApiParam(value = "工作状态code") @RequestParam(name = "code",required = false) String code);
}
具体接口实现(Controller)
public ApiResult updateUserStatus(@ApiParam(value = "用户id", required = true) @PathVariable(value = "userId") String userId,
@ApiParam(value = "工作状态code") @RequestParam(name = "code",required = false) String code) {
int result = statusService.updataUserStatus(userId,code);
if (result >= 0){
return Return2.success();
} else {
return Return2.paramError("api.uam.user.status.not.exist");
}
}
重点来了!
在test目录下创建测试类
添加相关注解
@RunWith(SpringRunner.class)
@SpringBootTest(classes= Application.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@EnableFeignClients(value = "link.test.client")
在client下建立一个client,继承测试的接口
@FeignClient(value="uam", url = RequiredParameters.LOCAL_URL + "/v1/users")
public interface UamUserStatusClient extends UamUserStatusApi {
}
建立Test类,将client引入
public class UamUserStatusTest extends BaseApplication {
@Autowired
private UamUserStatusClient statusClientclient;
private static String USER_ID = "00006b3b-04ab-11e8-b87b-d00d270cbb56";
private static final String code = "1";
@Test
public void updataUserStatusTest() {
ApiResult result = statusClientclient.updateUserStatus(USER_ID,code);
Assert.assertEquals(200, result.getCode());
}
}
在对应方法上,右键~debug!即可实现单元测试
完成~
注:以上仅提供简单实现步骤,具体还需结合项目实际具体配置!