Как настроить MockMvc для применения RequestPostProcessor ко всем запросам?JAVA

Программисты JAVA общаются здесь
Anonymous
Как настроить MockMvc для применения RequestPostProcessor ко всем запросам?

Сообщение Anonymous »

Дано:

Код: Выделить всё

public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(
authorize - >
authorize
.requestMatchers("/api/**")
.authenticated())
.oauth2ResourceServer(resourceServer -> resourceServer.jwt(withDefaults()))
.oauth2Login(withDefaults());

return http.build();
}

Код: Выделить всё

public class HelloControllerTests {

@Autowired private MockMvc mockMvc;

@Test
public void shouldReturnWorld() throws Exception {
this.mockMvc
.perform(get("/api/hello").with(oauth2Login()))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Hello, World!")));
}

@Test
public void shouldReturnBob() throws Exception {
this.mockMvc
.perform(get("/api/hello").with(oauth2Login()).param("name", "bob"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Hello, Bob")));
}
}
У меня есть большое количество тестов, и мне пришлось бы много повторений, чтобы включить with(oauth2Login()) в тесты конечных точек.
Как я могу сделать так, чтобыockMvc всегда включал with(oauth2Login()) во все запросы?

Подробнее здесь: https://stackoverflow.com/questions/788 ... l-requests

Вернуться в «JAVA»