Настройка Spring REST Docs не работает - java.lang.IllegalStateException - PullRequest
0 голосов
/ 23 октября 2019

@ 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?

Что я делаю не так? Сообщение об ошибке не очень информативно.

...