Search in sources :

Example 16 with AnnotationConfigServletWebApplicationContext

use of org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext in project spring-boot by spring-projects.

the class ConditionEvaluationReportLoggingListenerTests method canBeUsedInNonGenericApplicationContext.

@Test
void canBeUsedInNonGenericApplicationContext() {
    AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(Config.class);
    new ConditionEvaluationReportLoggingListener().initialize(context);
    context.refresh();
    assertThat(context.getBean(ConditionEvaluationReport.class)).isNotNull();
}
Also used : AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 17 with AnnotationConfigServletWebApplicationContext

use of org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext in project spring-boot by spring-projects.

the class ConditionalOnWebApplicationTests method testWebApplicationWithServletContext.

@Test
void testWebApplicationWithServletContext() {
    AnnotationConfigServletWebApplicationContext ctx = new AnnotationConfigServletWebApplicationContext();
    ctx.register(AnyWebApplicationConfiguration.class, ServletWebApplicationConfiguration.class, ReactiveWebApplicationConfiguration.class);
    ctx.setServletContext(new MockServletContext());
    ctx.refresh();
    this.context = ctx;
    assertThat(this.context.getBeansOfType(String.class)).containsExactly(entry("any", "any"), entry("servlet", "servlet"));
}
Also used : AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 18 with AnnotationConfigServletWebApplicationContext

use of org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext in project spring-boot by spring-projects.

the class SecurityFilterAutoConfigurationTests method filterAutoConfigurationWorksWithoutSecurityAutoConfiguration.

@Test
void filterAutoConfigurationWorksWithoutSecurityAutoConfiguration() {
    try (AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext()) {
        context.setServletContext(new MockServletContext());
        context.register(Config.class);
        context.refresh();
    }
}
Also used : AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 19 with AnnotationConfigServletWebApplicationContext

use of org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext in project spring-boot by spring-projects.

the class ControllerEndpointWebMvcIntegrationTests method endpointsCanBeAccessed.

@Test
void endpointsCanBeAccessed() throws Exception {
    TestSecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user", "N/A", "ROLE_ACTUATOR"));
    this.context = new AnnotationConfigServletWebApplicationContext();
    this.context.register(SecureConfiguration.class, ExampleController.class);
    TestPropertyValues.of("management.endpoints.web.base-path:/management", "management.endpoints.web.exposure.include=*").applyTo(this.context);
    MockMvc mockMvc = createSecureMockMvc();
    mockMvc.perform(get("/management/example")).andExpect(status().isOk());
}
Also used : AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.jupiter.api.Test)

Example 20 with AnnotationConfigServletWebApplicationContext

use of org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext in project spring-boot by spring-projects.

the class ControllerEndpointWebMvcIntegrationTests method endpointsAreSecureByDefault.

@Test
void endpointsAreSecureByDefault() throws Exception {
    this.context = new AnnotationConfigServletWebApplicationContext();
    this.context.register(SecureConfiguration.class, ExampleController.class);
    MockMvc mockMvc = createSecureMockMvc();
    mockMvc.perform(get("/actuator/example").accept(MediaType.APPLICATION_JSON)).andExpect(status().isUnauthorized());
}
Also used : AnnotationConfigServletWebApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.jupiter.api.Test)

Aggregations

AnnotationConfigServletWebApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext)23 Test (org.junit.jupiter.api.Test)16 MockServletContext (org.springframework.mock.web.MockServletContext)12 MockMvc (org.springframework.test.web.servlet.MockMvc)8 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)2 Filter (jakarta.servlet.Filter)1 FilterRegistration (jakarta.servlet.FilterRegistration)1 ServletContext (jakarta.servlet.ServletContext)1 ServletRegistration (jakarta.servlet.ServletRegistration)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 WebMvcEndpointHandlerMapping (org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping)1 PropertyPlaceholderAutoConfiguration (org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration)1 DispatcherFilter (org.springframework.boot.devtools.remote.server.DispatcherFilter)1 DeferredLinesWriter (org.springframework.boot.test.autoconfigure.web.servlet.SpringBootMockMvcBuilderCustomizer.DeferredLinesWriter)1 ReactiveWebApplicationContextRunner (org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner)1 WebApplicationContextRunner (org.springframework.boot.test.context.runner.WebApplicationContextRunner)1