Allow additional context interfaces to be defined for testing
Update `AssertableApplicationContext` and `ApplicationContextRunner` implementations to support additional `ApplicationContext` interfaces. Closes gh-42369
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context.assertj;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* Tests extra interface that can be applied to an {@link ApplicationContext}
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
interface AdditionalContextInterface {
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
/**
|
||||
* Tests for {@link AssertableApplicationContext}.
|
||||
@@ -38,4 +39,14 @@ class AssertableApplicationContextTests {
|
||||
assertThat(context).isInstanceOf(ConfigurableApplicationContext.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenHasAdditionalInterfaceShouldReturnProxy() {
|
||||
AssertableApplicationContext context = AssertableApplicationContext.get(
|
||||
() -> mock(ConfigurableApplicationContext.class,
|
||||
withSettings().extraInterfaces(AdditionalContextInterface.class)),
|
||||
AdditionalContextInterface.class);
|
||||
assertThat(context).isInstanceOf(ConfigurableApplicationContext.class)
|
||||
.isInstanceOf(AdditionalContextInterface.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebAppl
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
/**
|
||||
* Tests for {@link AssertableReactiveWebApplicationContext}.
|
||||
@@ -38,4 +39,14 @@ class AssertableReactiveWebApplicationContextTests {
|
||||
assertThat(context).isInstanceOf(ConfigurableReactiveWebApplicationContext.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenHasAdditionalInterfaceShouldReturnProxy() {
|
||||
AssertableReactiveWebApplicationContext context = AssertableReactiveWebApplicationContext.get(
|
||||
() -> mock(ConfigurableReactiveWebApplicationContext.class,
|
||||
withSettings().extraInterfaces(AdditionalContextInterface.class)),
|
||||
AdditionalContextInterface.class);
|
||||
assertThat(context).isInstanceOf(ConfigurableReactiveWebApplicationContext.class)
|
||||
.isInstanceOf(AdditionalContextInterface.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.withSettings;
|
||||
|
||||
/**
|
||||
* Tests for {@link AssertableWebApplicationContext}.
|
||||
@@ -38,4 +39,14 @@ class AssertableWebApplicationContextTests {
|
||||
assertThat(context).isInstanceOf(ConfigurableWebApplicationContext.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenHasAdditionalInterfaceShouldReturnProxy() {
|
||||
ConfigurableWebApplicationContext context = AssertableWebApplicationContext.get(
|
||||
() -> mock(ConfigurableWebApplicationContext.class,
|
||||
withSettings().extraInterfaces(AdditionalContextInterface.class)),
|
||||
AdditionalContextInterface.class);
|
||||
assertThat(context).isInstanceOf(ConfigurableWebApplicationContext.class)
|
||||
.isInstanceOf(AdditionalContextInterface.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -258,8 +258,16 @@ abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicati
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWirhAdditionalContextInterfaceHasCorrectInstanceOf() {
|
||||
getWithAdditionalContextInterface()
|
||||
.run((context) -> assertThat(context).isInstanceOf(AdditionalContextInterface.class));
|
||||
}
|
||||
|
||||
protected abstract T get();
|
||||
|
||||
protected abstract T getWithAdditionalContextInterface();
|
||||
|
||||
private static void throwCheckedException(String message) throws IOException {
|
||||
throw new IOException(message);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context.runner;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
/**
|
||||
* Tests extra interface that can be applied to an {@link ApplicationContext}
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
interface AdditionalContextInterface {
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.boot.test.context.runner;
|
||||
|
||||
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
/**
|
||||
* Tests for {@link ApplicationContextRunner}.
|
||||
@@ -33,4 +34,15 @@ class ApplicationContextRunnerTests extends
|
||||
return new ApplicationContextRunner();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ApplicationContextRunner getWithAdditionalContextInterface() {
|
||||
return new ApplicationContextRunner(TestAnnotationConfigApplicationContext::new,
|
||||
AdditionalContextInterface.class);
|
||||
}
|
||||
|
||||
static class TestAnnotationConfigApplicationContext extends AnnotationConfigApplicationContext
|
||||
implements AdditionalContextInterface {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2023 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.boot.test.context.runner;
|
||||
|
||||
import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext;
|
||||
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
|
||||
import org.springframework.boot.web.reactive.context.ConfigurableReactiveWebApplicationContext;
|
||||
|
||||
/**
|
||||
@@ -33,4 +34,15 @@ class ReactiveWebApplicationContextRunnerTests extends
|
||||
return new ReactiveWebApplicationContextRunner();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ReactiveWebApplicationContextRunner getWithAdditionalContextInterface() {
|
||||
return new ReactiveWebApplicationContextRunner(TestAnnotationConfigReactiveWebApplicationContext::new,
|
||||
AdditionalContextInterface.class);
|
||||
}
|
||||
|
||||
static class TestAnnotationConfigReactiveWebApplicationContext extends AnnotationConfigReactiveWebApplicationContext
|
||||
implements AdditionalContextInterface {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
* Copyright 2012-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.boot.test.context.runner;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
|
||||
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
|
||||
@@ -43,4 +44,15 @@ class WebApplicationContextRunnerTests extends
|
||||
return new WebApplicationContextRunner();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WebApplicationContextRunner getWithAdditionalContextInterface() {
|
||||
return new WebApplicationContextRunner(TestAnnotationConfigServletWebApplicationContext::new,
|
||||
AdditionalContextInterface.class);
|
||||
}
|
||||
|
||||
static class TestAnnotationConfigServletWebApplicationContext extends AnnotationConfigServletWebApplicationContext
|
||||
implements AdditionalContextInterface {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user