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 ))); }
相关推荐
在第4章里我们会看到如何为Spring Boot应用程序编写自动化集成测试。? 在第5章里你将看到一种有别于传统Java开发方式的做法,Spring Boot CLI能让你通过命令行来运行应用程序,这个应用程序完全是由Groovy脚本构成...
Spring Boot技术架构是一个全面而强大的框架,用于构建现代应用程序。该架构涵盖了前端、代理、API网关、业务服务、中间件、数据存储、持续集成和容器服务等关键组件,以提供完整的应用程序解决方案。 前端是用户与...
创建此示例项目是为了结合 spring-security 讨论正确的 spring-boot 应用程序配置。 当前状态 应用程序的安全配置和集成测试的安全配置。 感谢 Stack Overflow 的 Kris,该应用程序现在按预期工作。 解决方案 如 ...
通过实际测试对比了Spring Boot中6种MySQL批量更新方式的效率,并详细记录了每种方法在处理500,1000,5000,10000,50000,100000条数据的平均时间、最小时间和最大时间。包括MyBatis-Plus提供的批量更新,JdbcTemplate...
针对不同的Web应用程序身份验证方案测试应用程序在这里,您可以找到适用于不同身份验证方案的简单spring-boot应用程序。 创建此回购协议是为了针对不同的身份验证方案测试 。 这是我的GSoC项目的一部分。 保留了各种...
参考文献Sprint Boot通过Spring Boot,可以轻松创建具有Spring支持的生产级应用程序和服务,而不必大惊小怪。 它从Spring平台的角度出发,以便新老用户都能快速找到所需的信息。 参考: : Spring Security SAML扩展...
在第4章里我们会看到如何为Spring Boot应用程序编写自动化集成测试。 在第5章里你将看到一种有别于传统Java开发方式的做法,Spring Boot CLI能让你通过命 令行来运行应用程序,这个应用程序完全是由Groovy脚本...
使用SpringBoot构建一个微服务体系结构,方法是将一个应用程序从一个小的Monolith演化为一个由多个服务组成的事件驱动的体系结构。这本书遵循一个渐进的方法来教授微服务结构,测试驱动的开发,尤里卡,丝带,祖尔,...
在第4章里我们会看到如何为Spring Boot应用程序编写自动化集成测试。在第5章里你将看到一种有别于传统Java开发方式的做法,Spring Boot CLI能让你通过命令行来运行应用程序,这个应用程序完全是由Groovy脚本构成的。...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...
摘要:本论文介绍了一种基于Spring Boot和Vue.js的Web应用程序开发框架,旨在提供一个通用的管理系统模板。通过系统的需求分析、概要设计、详细设计和测试等阶段,可以全面了解该框架的设计思路和实现细节,掌握相关...