【发布时间】:2020-10-29 20:14:36
【问题描述】:
大多数时候,我们不是在普通的JUnit 断言中添加 cmets,而是在断言中添加一条消息,以解释为什么这是断言在它所在的位置:
Person p1 = new Person("Bob");
Person p2 = new Person("Bob");
assertEquals(p1, p2, "Persons with the same name should be equal.");
现在,当涉及到 Spring Boot Web 环境中的端点测试时,我最终得到了:
// Bad request because body not posted
mockMvc.perform(post("/postregistration")).andExpect(status().isBadRequest());
// Body posted, it should return OK
mockMvc.perform(post("/postregistration").content(toJson(registrationDto))
.andExpect(status().isOk()));
有没有办法摆脱 cmets 并为这种断言添加消息?所以,当测试失败时,我会看到消息。
【问题讨论】:
-
一个想法是调用failIfNoBodyPosted之类的测试方法,这样当测试失败时你就知道原因了。
-
@marc 嗯,是的,我以前想过,但我不喜欢它。测试一个有 10 个端点的控制器,每个端点有 5-6 个测试用例,我最终会得到 60 个名称相似的方法,并且我将无法知道哪个方法引用了哪个端点测试。就个人而言,当涉及到(简单的)端点测试用例时,我喜欢类似脚本的代码。
标签: java spring-boot junit mockmvc