遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

SpringBoot开发mockserver及生成swagger接口文档

通过springboot开发mock server,包含get及post接口,用于练习接口自动化及jmeter很方便

当然,也为后面jenkins持续集成做基础(开发push代码后  → jenkins自动构建  → 打包  → 重启服务器  → 自动执行自动化测试脚本  → 生成测试报告  → 发送邮件)

项目目录

接口

package com.uncleyong.testdemo.service;

import com.uncleyong.testdemo.model.User;


public interface UserService {
    Object queryUserById(int id);
    Object deleteUserById(int id);
    Object updateUserById(int id, String department);
    Object addUser(int id, String name, String sex, String department);
}

实现

package com.uncleyong.testdemo.service.impl;

import com.uncleyong.testdemo.model.JsonData;
import com.uncleyong.testdemo.model.User;
import com.uncleyong.testdemo.service.UserService;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Override
    public Object queryUserById(int id) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>查询用户");
        User user = new User();
        user.setId(id);
        user.setName("jack" + id);
        user.setSex("男");
        user.setDepartment("测试开发部");
        return JsonData.querySuccess(user);
    }


    @Override
    public Object deleteUserById(int id) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>删除用户");
        User user = new User();
        user.setId(id);
        user.setName("jack" + id);
        user.setSex("男");
        user.setDepartment("测试开发部");
        return JsonData.deleteSuccess(user);
    }

    @Override
    public Object updateUserById(int id, String department) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>更新用户");
        User user = new User();
        user.setId(id);
        user.setName("jack" + id);
        user.setSex("男");
        user.setDepartment(department);
        return JsonData.updateFail(user);
    }

    @Override
    public Object addUser(int id, String name, String sex, String department) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>新增用户");
        System.out.println("id:" + id + ", name:" + name + ", sex:" + sex + ", department:" + department);
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setSex(sex);
        user.setDepartment(department);
        return JsonData.addSuccess(user);
    }
}

controller

package com.uncleyong.testdemo.controller;

import com.uncleyong.testdemo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/v1/user")
@Api(value = "/api/v1/user", description = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    @ApiOperation(value = "查询用户",httpMethod = "GET")
    public Object qryUser(@RequestParam(name="userId")int userId){
        return userService.queryUserById(userId);
    }


    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "删除用户",httpMethod = "POST")
    public Object delUser(@RequestParam(name="userId")int userId){
        return userService.deleteUserById(userId);
    }

    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    @ApiOperation(value = "更新用户",httpMethod = "POST")
    public Object updateUser(@RequestParam(name="userId")int userId, @RequestParam(name="department")String department){
        return userService.updateUserById(userId, department);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增用户",httpMethod = "POST")
    public Object addUser(@RequestParam(name="userId")int userId, @RequestParam(name="name")String name, @RequestParam(defaultValue="男",name="sex")String sex, @RequestParam(name="department")String department){
        return userService.addUser(userId, name, sex, department);
    }
}

swagger配置

package com.uncleyong.testdemo.config;


import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;



@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .paths(PathSelectors.regex("/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("testdemo项目接口文档")
                .contact(new Contact("【uncleyong】","",""))
                .description("这是swaggerui生成的testdemo项目接口文档")
                .license("项目源码地址").licenseUrl("https://www.cnblogs.com/uncleyong/p/10530261.html")
                .version("1.0.0.0")
                .build();
    }
}

运行Main文件,启动服务

生成的swagger接口文档

浏览器访问接口

  

 

https://gitee.com/uncleyong/javaMockServer

 

posted @ 2019-05-07 23:10  全栈测试笔记  阅读(2831)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end