欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot的应用程序单元测试方案

程序员文章站 2022-07-13 08:02:42
...

https://www.jianshu.com/p/2e84bd5dc9d6 (基于spring-boot的应用程序的单元测试方案)

 

 spring mock测试的时候,可以根据spring docs 和MockMvc应用结合起来自动生成接口api文档;

 

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringRestDocsCommTest {
    //公共参数
protected static final ParameterDescriptor REQUEST_ID = parameterWithName("id").description("数据ID,不能为空");
    protected static final ParameterDescriptor REQUEST_PAGE_NUMBER = parameterWithName("pageNum").description("页码,第一页页码为1,默认值为1");
    protected static final ParameterDescriptor REQUEST_PAGE_SIZE = parameterWithName("pageSize").description("页长,默认值为10");
    protected static final FieldDescriptor REQUEST_OPT_LOCK = fieldWithPath("optLock").type("Integer").description("数据库乐观锁,不能为空");
//公共响应
protected static final FieldDescriptor RESPONSE_OPT_LOCK = fieldWithPath("optLock").type("Integer").description("数据库乐观锁");
    protected static final FieldDescriptor RESPONSE_PAGE_NUMBER = fieldWithPath("pageNum").type("Integer").description("页码");
    protected static final FieldDescriptor RESPONSE_PAGE_SIZE = fieldWithPath("pageSize").type("Integer").description("页长");
    protected static final FieldDescriptor RESPONSE_SIZE = fieldWithPath("size").type("Integer").description("查询总数");
    protected static final FieldDescriptor RESPONSE_START_ROW = fieldWithPath("startRow").type("Integer").description("初始行数");
    protected static final FieldDescriptor RESPONSE_END_ROW = fieldWithPath("endRow").type("Integer").description("结尾行数");
    protected static final FieldDescriptor RESPONSE_TOTAL = fieldWithPath("total").type("Integer").description("总数");
    protected static final FieldDescriptor RESPONSE_PAGES = fieldWithPath("pages").type("Integer").description("总页数");
    protected static final FieldDescriptor RESPONSE_PRE_PAGE = fieldWithPath("prePage").type("Integer").description("前页页数");
    protected static final FieldDescriptor RESPONSE_NEXT_PAGE = fieldWithPath("nextPage").type("Integer").description("后页页数");
    protected static final FieldDescriptor RESPONSE_IS_FIRST_PAGE = fieldWithPath("isFirstPage").type("Boolean").description("是否是第一页");
    protected static final FieldDescriptor RESPONSE_IS_LAST_PAGE = fieldWithPath("isLastPage").type("Boolean").description("是否是最后一页");
    protected static final FieldDescriptor RESPONSE_HAS_PRE_PAGE = fieldWithPath("hasPreviousPage").type("Boolean").description("是否还有前页");
    protected static final FieldDescriptor RESPONSE_HAS_NEXT_PAGE = fieldWithPath("hasNextPage").type("Boolean").description("是否还有后页");
    protected static final FieldDescriptor RESPONSE_NAV_PAGES = fieldWithPath("navigatePages").type("Integer").description("导航页码数");
    protected static final FieldDescriptor RESPONSE_NAV_PAGE_NUMBER = fieldWithPath("navigatepageNums").type("Integer[]").description("所有导航页号");
    protected static final FieldDescriptor RESPONSE_NAV_FIRST_PAGE = fieldWithPath("navigateFirstPage").type("Integer").description("导航第一页");
    protected static final FieldDescriptor RESPONSE_NAV_LAST_PAGE = fieldWithPath("navigateLastPage").type("Integer").description("导航最后页");
    protected static final FieldDescriptor RESPONSE_LAST_PAGE = fieldWithPath("lastPage").type("Integer").description("最后页数");
    protected static final FieldDescriptor RESPONSE_FIRST_PAGE = fieldWithPath("firstPage").type("Integer").description("第一页数");
    protected static final FieldDescriptor RESPONSE_CREATE_TIME = fieldWithPath("createTime").type("Date").description("创建时间");
    protected static final FieldDescriptor RESPONSE_UPDATE_TIME = fieldWithPath("updateTime").type("Date").description("更新时间");
    private String host = "localhost";
    private int port = 8080;
    private String scheme = "http";
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation();
    protected MockMvc mockMvc;
@Autowired
protected ObjectMapper objectMapper;
@Autowired
private WebApplicationContext context;
@Before
public void setUp() {
        Snippet[] defaultSnippets = new Snippet[]{CliDocumentation.curlRequest(), CliDocumentation.httpieRequest(), HttpDocumentation.httpRequest(), HttpDocumentation.httpResponse(), PayloadDocumentation.requestBody(), PayloadDocumentation.responseBody(), HeaderDocumentation.requestHeaders()};
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
                .apply(documentationConfiguration(this.restDocumentation)
                        .snippets().withTemplateFormat(TemplateFormats.asciidoctor()).withEncoding("UTF-8")
                        .withDefaults(defaultSnippets)
                        .and()
                        .uris().withScheme(scheme).withHost(host).withPort(port)
                        .and()
                )
                .build();
}

    @Test
public void test() {
        System.out.println("----SpringRestDocsTest:" + host + "---" + port + "---" + scheme);
}
}

 

具体的测试类生成如下:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class UserRestServiceTest extends SpringRestDocsCommTest {
    @Test
public void a_getUserList() throws Exception {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("pageNum", "1");
params.add("pageSize", "5");
params.add("status", "0");
        this.mockMvc.perform(get("/v1/users").params(params))
                .andExpect(status().isOk())
                .andDo(document("users-list", requestParameters(
                        REQUEST_PAGE_NUMBER,
REQUEST_PAGE_SIZE,
parameterWithName("status").description("用户状态,可以为空")),
responseFields(
                                subsectionWithPath("list").type("System对象数组").description("用户列表,具体数据参考根据ID获取用户接口"),
RESPONSE_PAGE_NUMBER,
RESPONSE_PAGE_SIZE,
RESPONSE_SIZE,
RESPONSE_START_ROW,
RESPONSE_END_ROW,
RESPONSE_TOTAL,
RESPONSE_PAGES,
RESPONSE_PRE_PAGE,
RESPONSE_NEXT_PAGE,
RESPONSE_IS_FIRST_PAGE,
RESPONSE_IS_LAST_PAGE,
RESPONSE_HAS_PRE_PAGE,
RESPONSE_HAS_NEXT_PAGE,
RESPONSE_NAV_PAGES,
RESPONSE_NAV_PAGE_NUMBER,
RESPONSE_NAV_FIRST_PAGE,
RESPONSE_NAV_LAST_PAGE/*,
                                RESPONSE_LAST_PAGE,       // 这2个参数在新版本已经被取消了
                                RESPONSE_FIRST_PAGE*/)));
}


    @Test
public void b_getUserById() throws Exception {
        this.mockMvc.perform(
                get("/v1/users/{id}", 1))
                .andExpect(status().isOk())
                .andDo(document("users-get",
pathParameters(REQUEST_ID),
responseFields(
                                subsectionWithPath("id").type("Long").description("用户ID"),
subsectionWithPath("name").type("String").description("用户姓名"),
subsectionWithPath("age").type("Integer").description("用户年龄"),
subsectionWithPath("status").type("Integer").description("用户状态"),
subsectionWithPath("description").type("String").description("描述信息"),
RESPONSE_CREATE_TIME,
RESPONSE_UPDATE_TIME,
RESPONSE_OPT_LOCK
)));
}

 

 

相关标签: spring boot