Spring REST文档配置不工作-java.lang.IllegalStateException
@AutoConfigureRestDocs和@AutoConfigureMockMvc未正确配置MockMvc。即使手动配置它们似乎也没有帮助 我还尝试手动配置MockMvc和MockMvcRestDocumentationConfigurer,但没有帮助 这是当前设置:Spring REST文档配置不工作-java.lang.IllegalStateException,java,spring,spring-security,spring-test,spring-restdocs,Java,Spring,Spring Security,Spring Test,Spring Restdocs,@AutoConfigureRestDocs和@AutoConfigureMockMvc未正确配置MockMvc。即使手动配置它们似乎也没有帮助 我还尝试手动配置MockMvc和MockMvcRestDocumentationConfigurer,但没有帮助 这是当前设置: @RunWith(SpringRunner.class) @SpringBootTest(properties= "spring.main.allow-bean-definition-overriding=true") @A
@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?
我做错了什么?错误消息的信息量不大。为了避免任何人浪费时间尝试回答此问题,还将其交叉发布到GitHub上的REST Docs问题跟踪器:。根本原因是Spring安全性和由
formLogin()
创建的请求生成器的错误/限制。为了避免任何人浪费时间尝试回答这个问题,它还交叉发布到GitHub上的REST文档问题跟踪程序:。根本原因是Spring安全性和由formLogin()
创建的请求生成器的缺陷/限制。