/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
spring-boot-modules/spring-boot-security/src/test/java/com/baeldung/integrationtesting/SecuredControllerWebMvcIntegrationTest.java
32 строки
1 KB
Harry9656
JAVA-31190: Preparation for migrating spring-boot-modules to version 3. (#15834)
27 фев 2024, 04:49
Не верифицирован
27 фев 2024, 04:49
aefd833
Код
Авторство
О чём код?
package com.baeldung.integrationtesting; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; @WebMvcTest(SecuredController.class) class SecuredControllerWebMvcIntegrationTest { @Autowired private MockMvc mvc; @Test void givenRequestOnPrivateService_shouldFailWith401() throws Exception { mvc.perform(get("/private/hello").contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isUnauthorized()); } @WithMockUser(value = "spring") @Test void givenAuthRequestOnPrivateService_shouldSucceedWith200() throws Exception { mvc.perform(get("/private/hello").contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()); } }