java - Spring REST Docs 配置不起作用 - java.lang.IllegalStateException
问题描述
@AutoConfigureRestDocs 和 @AutoConfigureMockMvc 没有正确配置 MockMvc。即使手动配置它们似乎也无济于事。
我也尝试手动配置 MockMvc 和 MockMvcRestDocumentationConfigurer,但没有帮助。
这是当前的设置:
@RunWith(SpringRunner.class)
@SpringBootTest(properties= "spring.main.allow-bean-definition-overriding=true")
@AutoConfigureRestDocs
@AutoConfigureMockMvc
public class LoginLogoutTest {
@Autowired
private MockMvc mockMvc;
@Test
public void adminCanLoginLogout() throws Exception {
mockMvc.perform(formLogin().user(TestConfig.ADMIN_USERNAME).password(TestConfig.PASSWORD))
.andExpect(status().isOk())
.andExpect(authenticated().withUsername(TestConfig.ADMIN_USERNAME))
.andDo(document("login"));
mockMvc.perform(logout())
.andExpect(status().isOk())
.andExpect(unauthenticated())
.andDo(document("logout"));
}
}
我还尝试使用以下方式配置它们:
@RunWith(SpringRunner.class)
@SpringBootTest(properties= "spring.main.allow-bean-definition-overriding=true")
public class LoginLogoutTest {
@Rule
public JUnitRestDocumentation restDocumentation = new JUnitRestDocumentation("target/generated-snippets");
private MockMvc mockMvc;
@Before
public void setUp(){
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context)
.apply(documentationConfiguration(this.restDocumentation))
.build();
}
@Test
public void adminCanLoginLogout() throws Exception {
mockMvc.perform(formLogin().user(TestConfig.ADMIN_USERNAME).password(TestConfig.PASSWORD))
.andExpect(status().isOk())
.andExpect(authenticated().withUsername(TestConfig.ADMIN_USERNAME))
.andDo(document("login"));
mockMvc.perform(logout())
.andExpect(status().isOk())
.andExpect(unauthenticated())
.andDo(document("logout"));
}
}
我收到以下错误:
java.lang.IllegalStateException: REST Docs configuration not found. Did you forget to apply a MockMvcRestDocumentationConfigurer when building the MockMvc instance?
我究竟做错了什么?错误消息不是很丰富。
解决方案
推荐阅读
- php - 如何在常量文件 laravel 中访问 $_SERVER['SERVER_NAME']
- python - pyqt按需加载graphicsspixmapitem
- c# - 在客户端计算机上运行的 Crystal Reports(没有 CRRuntime)
- delphi - 安装 Delphi 包会导致错误丢失 BDS.INI
- ibm-cloud-infrastructure - 如何在 SoftLayer 票证上设置附加设备的密码
- node.js - package-lock.json 会阻止更新吗?
- ruby-on-rails - Ruby on Rails 5 Action Mailer 的错误
- java - 将数据从 kotlin 活动传递到 java DialogFragment
- python - Pandas:使用 python map() 意外输出数据类型
- angular - angular-file-uploader 附加参数