java - 如何使用 Junit 5 测试 Spring Boot Rest 控制器
问题描述
我很难找到关于如何为 Sprint Boot 实际编写 Junit5 测试代码的一致资源。现在,我有一个非常简单的控制器并尝试进行 Junit5 测试,但我什至无法从简单的休息调用中获得结果。假设返回一个字符串值数组。我认为主要问题是 MockHttpServletResponse 标头没有在其中附加端口,但我希望它可以动态添加,因此无论谁拉下代码,Junit5 都可以对其进行测试。
@RestController
public class CnlLetterController {
@Autowired
private CnlLetterService cnlLetterService;
@Autowired
private UserService userService;
@Autowired
private ServiceLetterService serviceLetterService;
@GetMapping("/rest/cnl/templates")
public List<String> findTemplates () {
return cnlLetterService.getTemplates();
}
/* Other methods that don't matter right now */
}
测试控制器
@ActiveProfiles("dev")
@ExtendWith(SpringExtension.class)
@WebMvcTest(CnlLetterController.class)
class CnlLetterControllerTest {
@MockBean
private ClientRegistrationRepository crr;
@MockBean
private CnlLetterService cnlLetterService;
@MockBean
private UserService userService;
@MockBean
private ServiceLetterService serviceLetterService;
@Autowired
private MockMvc mockMvc;
@Test
void testFindTemplates() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/pis/rest/cnl/templates")
.accept(MediaType.APPLICATION_JSON))
.andDo(print()).andExpect(status().isOk());
}
}
Junit控制台输出
MockHttpServletRequest:
HTTP Method = GET
Request URI = /pis/rest/cnl/templates
Parameters = {}
Headers = [Accept:"application/json"]
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = null
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 302
Error message = null
Headers = [Location:"https://localhost/pis/rest/cnl/templates"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = https://localhost/pis/rest/cnl/templates
Cookies = []
MockHttpServletRequest:
HTTP Method = GET
Request URI = /pis/rest/cnl/templates
Parameters = {}
Headers = [Accept:"application/json"]
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = null
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 302
Error message = null
Headers = [Location:"https://localhost/pis/rest/cnl/templates"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = https://localhost/pis/rest/cnl/templates
Cookies = []
2020-07-20 13:37:29.648 INFO 12868 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
解决方案
推荐阅读
- python - 使用 XlsxWriter 将文本框或文本框中的文本定向为垂直?
- java - 如何制作使用任何窗格的基本 GUI 功能
- python - Python 简化梯队形式
- cookies - 关于 xss 和 csrf 的文章似乎是错误的
- reactjs - 更新状态时更新输入值reactjs
- sql - 分组结果 - SQL
- java - 如何修复 RapidClipse4 org.hibernate.MappingException:无法从文件配置覆盖:hibernate.reveng.xml
- python - 在数据框更改时更新字典键/值对
- javascript - Extjs 表单和文本区域无法处理带尖括号('<>')的输入。奇怪的行为,用户界面就死了
- java - 无法从非@Nullable @Provides 方法返回 null