|
1 | 1 | /* |
2 | | - * Copyright 2012-2017 the original author or authors. |
| 2 | + * Copyright 2012-2018 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.condition; |
18 | 18 |
|
19 | | -import org.junit.After; |
20 | 19 | import org.junit.Test; |
21 | 20 | import reactor.core.publisher.Mono; |
22 | 21 |
|
23 | 22 | import org.springframework.boot.autoconfigure.web.reactive.MockReactiveWebServerFactory; |
24 | | -import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; |
| 23 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 24 | +import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; |
| 25 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
25 | 26 | import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; |
26 | | -import org.springframework.context.ConfigurableApplicationContext; |
27 | | -import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
28 | 27 | import org.springframework.context.annotation.Bean; |
29 | 28 | import org.springframework.context.annotation.Configuration; |
30 | 29 | import org.springframework.http.server.reactive.HttpHandler; |
31 | | -import org.springframework.mock.web.MockServletContext; |
32 | | -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
33 | 30 |
|
34 | 31 | import static org.assertj.core.api.Assertions.assertThat; |
35 | 32 | import static org.assertj.core.api.Assertions.entry; |
|
42 | 39 | */ |
43 | 40 | public class ConditionalOnNotWebApplicationTests { |
44 | 41 |
|
45 | | - private ConfigurableApplicationContext context; |
46 | | - |
47 | | - @After |
48 | | - public void closeContext() { |
49 | | - if (this.context != null) { |
50 | | - this.context.close(); |
51 | | - } |
52 | | - } |
53 | | - |
54 | 42 | @Test |
55 | 43 | public void testNotWebApplicationWithServletContext() { |
56 | | - AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); |
57 | | - ctx.register(NotWebApplicationConfiguration.class); |
58 | | - ctx.setServletContext(new MockServletContext()); |
59 | | - ctx.refresh(); |
60 | | - this.context = ctx; |
61 | | - assertThat(this.context.getBeansOfType(String.class)).isEmpty(); |
| 44 | + new WebApplicationContextRunner() |
| 45 | + .withUserConfiguration(NotWebApplicationConfiguration.class) |
| 46 | + .run((context) -> assertThat(context).doesNotHaveBean(String.class)); |
62 | 47 | } |
63 | 48 |
|
64 | 49 | @Test |
65 | 50 | public void testNotWebApplicationWithReactiveContext() { |
66 | | - AnnotationConfigReactiveWebApplicationContext context = new AnnotationConfigReactiveWebApplicationContext(); |
67 | | - context.register(ReactiveApplicationConfig.class, |
68 | | - NotWebApplicationConfiguration.class); |
69 | | - context.refresh(); |
70 | | - this.context = context; |
71 | | - assertThat(this.context.getBeansOfType(String.class)).isEmpty(); |
| 51 | + new ReactiveWebApplicationContextRunner() |
| 52 | + .withUserConfiguration(ReactiveApplicationConfig.class, |
| 53 | + NotWebApplicationConfiguration.class) |
| 54 | + .run((context) -> assertThat(context).doesNotHaveBean(String.class)); |
72 | 55 | } |
73 | 56 |
|
74 | 57 | @Test |
75 | 58 | public void testNotWebApplication() { |
76 | | - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
77 | | - ctx.register(NotWebApplicationConfiguration.class); |
78 | | - ctx.refresh(); |
79 | | - this.context = ctx; |
80 | | - assertThat(this.context.getBeansOfType(String.class)) |
81 | | - .containsExactly(entry("none", "none")); |
| 59 | + new ApplicationContextRunner() |
| 60 | + .withUserConfiguration(NotWebApplicationConfiguration.class) |
| 61 | + .run((context) -> assertThat(context).getBeans(String.class) |
| 62 | + .containsExactly(entry("none", "none"))); |
82 | 63 | } |
83 | 64 |
|
84 | 65 | @Configuration |
|
0 commit comments