<!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> |
@RequestMapping(value = "/login", method = RequestMethod.POST) public ResponseBean authLogin(@RequestBody LoginVo login) { if (login == null) throw new ClientException(ErrorCode.API_EC_CLIENT_EXCEPTION); if (StringUtils.isEmpty(login.getUsername())) throw new ClientException(ErrorCode.API_EC_CLIENT_EXCEPTION); if (StringUtils.isEmpty(login.getPassword())) throw new ClientException(ErrorCode.API_EC_CLIENT_EXCEPTION); LoginResultBean token = userService.login(login); return ResponseBean.success(token); } |
@RunWith(SpringRunner.class) @SpringBootTest(classes = { LoginController.class, MockServletContext.class }) @EnableAutoConfiguration @ComponentScan(basePackages= {"com.offcn.*"}) @WebAppConfiguration ##声明测试加载WebApplicationContext public class LoginTest { @Autowired WebApplicationContext webApplicationContext; private MockMvc mockMvc; @Before public void SetUp(){ this.mockMvc= MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void login() throws Exception { RequestBuilder request = null; LoginVo loginVo=new LoginVo(); loginVo.setUsername("admin"); loginVo.setPassword("admin"); ##构造请求体JSON格式 String LoginVoJson=JSONObject.toJSONString(loginVo); request = MockMvcRequestBuilders.post("/v1/login") .contentType(MediaType.APPLICATION_JSON) .content(LoginVoJson); String requestJson = mockMvc.perform(request).andDo(print()) .andExpect(status().isOk()).andReturn().getResponse().getContentAsString(); System.out.println(requestJson); } } |
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version> </dependency> |
@Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurerAdapter { @Value("${swagger.host}") private String swaggerHost; ApiInfo apiInfo() { return new ApiInfoBuilder() .title("swagger接口文档") .description("基于shiro和jwt的前后端分离权限系统") .termsOfServiceUrl("") .version("v1") .contact(new Contact("", "", "")) .build(); } @Bean public Docket createRestApi() { ParameterBuilder builder = new ParameterBuilder(); Parameter parameter = builder // 从cookie中获取token .parameterType("cookie") //参数类型支持header, cookie, body, query etc .name("token") //参数名 .defaultValue("") //默认值 .description("请输入token") .modelRef(new ModelRef("string")) //指定参数值的类型 .required(false).build(); //非必需,这里是全局配置,然而在登陆的时候是不用验证的 List<Parameter> parameters = Lists.newArrayList(parameter); return new Docket(DocumentationType.SWAGGER_2) .host(this.swaggerHost) .select() .apis(RequestHandlerSelectors.basePackage("com.offcn.login.controller")) .paths(PathSelectors.any()) .build() .apiInfo(this.apiInfo()) .globalOperationParameters(parameters); } /** * swagger ui资源映射 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars*") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * swagger-ui.html路径映射,浏览器中使用/api-docs访问 * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/api-docs","/swagger-ui.html"); } } |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |