Browse Source
This commit makes sure that `@SpringBootTest` with a reactive setup starts a web application if necessary. If both a servlet and a reactive environment are available, a servlet environment is bootstraped. This commit also adds a way to force a reactive environment by specifying the `spring.main.web-application-type` property of the test (e.g. `@TestPropertySource`). Closes gh-8383pull/8410/head
12 changed files with 291 additions and 12 deletions
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
/* |
||||
* Copyright 2012-2017 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 |
||||
* |
||||
* http://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; |
||||
|
||||
import org.springframework.test.context.MergedContextConfiguration; |
||||
|
||||
/** |
||||
* Encapsulates the <em>merged</em> context configuration declared on a test class and |
||||
* all of its superclasses for a reactive web application. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
class ReactiveWebMergedContextConfiguration extends MergedContextConfiguration { |
||||
|
||||
ReactiveWebMergedContextConfiguration( |
||||
MergedContextConfiguration mergedConfig) { |
||||
super(mergedConfig); |
||||
} |
||||
} |
||||
@ -0,0 +1,101 @@
@@ -0,0 +1,101 @@
|
||||
/* |
||||
* Copyright 2012-2017 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 |
||||
* |
||||
* http://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; |
||||
|
||||
import org.junit.Test; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.boot.context.embedded.LocalServerPort; |
||||
import org.springframework.boot.context.embedded.ReactiveWebApplicationContext; |
||||
import org.springframework.boot.context.embedded.ReactiveWebServerFactory; |
||||
import org.springframework.boot.context.embedded.tomcat.TomcatReactiveWebServerFactory; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; |
||||
import org.springframework.http.server.reactive.HttpHandler; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.client.RestTemplate; |
||||
import org.springframework.web.server.adapter.WebHttpHandlerBuilder; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Base class for {@link SpringBootTest} tests configured to start an embedded reactive |
||||
* container. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
public abstract class AbstractSpringBootTestEmbeddedReactiveWebEnvironmentTests { |
||||
|
||||
@LocalServerPort |
||||
private int port = 0; |
||||
|
||||
@Value("${value}") |
||||
private int value = 0; |
||||
|
||||
@Autowired |
||||
private ReactiveWebApplicationContext context; |
||||
|
||||
public ReactiveWebApplicationContext getContext() { |
||||
return this.context; |
||||
} |
||||
|
||||
@Test |
||||
public void runAndTestHttpEndpoint() { |
||||
assertThat(this.port).isNotEqualTo(8080).isNotEqualTo(0); |
||||
String body = new RestTemplate() |
||||
.getForObject("http://localhost:" + this.port + "/", String.class); |
||||
assertThat(body).isEqualTo("Hello World"); |
||||
} |
||||
|
||||
@Test |
||||
public void annotationAttributesOverridePropertiesFile() throws Exception { |
||||
assertThat(this.value).isEqualTo(123); |
||||
} |
||||
|
||||
protected static class AbstractConfig { |
||||
|
||||
@Value("${server.port:8080}") |
||||
private int port = 8080; |
||||
|
||||
@Bean |
||||
public HttpHandler httpHandler(ApplicationContext applicationContext) { |
||||
return WebHttpHandlerBuilder.applicationContext(applicationContext).build(); |
||||
} |
||||
|
||||
@Bean |
||||
public ReactiveWebServerFactory embeddedReactiveContainer() { |
||||
TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(); |
||||
factory.setPort(this.port); |
||||
return factory; |
||||
} |
||||
|
||||
@Bean |
||||
public static PropertySourcesPlaceholderConfigurer propertyPlaceholder() { |
||||
return new PropertySourcesPlaceholderConfigurer(); |
||||
} |
||||
|
||||
@RequestMapping("/") |
||||
public Mono<String> home() { |
||||
return Mono.just("Hello World"); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/* |
||||
* Copyright 2012-2017 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 |
||||
* |
||||
* http://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; |
||||
|
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
import org.springframework.web.reactive.config.EnableWebFlux; |
||||
|
||||
/** |
||||
* Tests for {@link SpringBootTest} in a reactive environment configured |
||||
* with {@link WebEnvironment#DEFINED_PORT}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@DirtiesContext |
||||
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, properties = { |
||||
"spring.main.web-application-type=reactive", "server.port=0", "value=123" }) |
||||
public class SpringBootTestReactiveWebEnvironmentDefinedPortTests |
||||
extends AbstractSpringBootTestEmbeddedReactiveWebEnvironmentTests { |
||||
|
||||
@Configuration |
||||
@EnableWebFlux |
||||
@RestController |
||||
protected static class Config extends AbstractConfig { |
||||
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/* |
||||
* Copyright 2012-2017 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 |
||||
* |
||||
* http://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; |
||||
|
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
import org.springframework.web.reactive.config.EnableWebFlux; |
||||
|
||||
/** |
||||
* Tests for {@link SpringBootTest} in a reactive environment configured |
||||
* with {@link WebEnvironment#RANDOM_PORT}. |
||||
* |
||||
* @author Stephane Nicoll |
||||
*/ |
||||
@RunWith(SpringRunner.class) |
||||
@DirtiesContext |
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { |
||||
"spring.main.webApplicationType=reactive", "value=123" }) |
||||
public class SpringBootTestReactiveWebEnvironmentRandomPortTests |
||||
extends AbstractSpringBootTestEmbeddedReactiveWebEnvironmentTests { |
||||
|
||||
@Configuration |
||||
@EnableWebFlux |
||||
@RestController |
||||
protected static class Config extends AbstractConfig { |
||||
|
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue