|
16 | 16 |
|
17 | 17 | package sample.data.elasticsearch;
|
18 | 18 |
|
19 |
| -import java.net.ConnectException; |
| 19 | +import java.io.File; |
20 | 20 |
|
| 21 | +import org.junit.ClassRule; |
21 | 22 | import org.junit.Rule;
|
22 | 23 | import org.junit.Test;
|
| 24 | +import org.junit.rules.TestRule; |
| 25 | +import org.junit.runner.Description; |
| 26 | +import org.junit.runners.model.Statement; |
23 | 27 |
|
24 | 28 | import org.springframework.boot.builder.SpringApplicationBuilder;
|
25 | 29 | import org.springframework.boot.test.rule.OutputCapture;
|
26 |
| -import org.springframework.core.NestedCheckedException; |
27 | 30 |
|
28 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
29 | 32 |
|
|
33 | 36 | * @author Artur Konczak
|
34 | 37 | */
|
35 | 38 | public class SampleElasticsearchApplicationTests {
|
| 39 | + |
36 | 40 | @Rule
|
37 | 41 | public OutputCapture outputCapture = new OutputCapture();
|
38 | 42 |
|
| 43 | + @ClassRule |
| 44 | + public static SkipOnWindows skipOnWindows = new SkipOnWindows(); |
| 45 | + |
39 | 46 | @Test
|
40 | 47 | public void testDefaultSettings() throws Exception {
|
41 |
| - try { |
42 |
| - new SpringApplicationBuilder(SampleElasticsearchApplication.class).run(); |
43 |
| - } |
44 |
| - catch (IllegalStateException ex) { |
45 |
| - if (serverNotRunning(ex)) { |
46 |
| - return; |
47 |
| - } |
48 |
| - } |
| 48 | + new SpringApplicationBuilder(SampleElasticsearchApplication.class).run(); |
49 | 49 | String output = this.outputCapture.toString();
|
50 | 50 | assertThat(output).contains("firstName='Alice', lastName='Smith'");
|
51 | 51 | }
|
52 | 52 |
|
53 |
| - private boolean serverNotRunning(IllegalStateException ex) { |
54 |
| - @SuppressWarnings("serial") |
55 |
| - NestedCheckedException nested = new NestedCheckedException("failed", ex) { |
56 |
| - }; |
57 |
| - if (nested.contains(ConnectException.class)) { |
58 |
| - Throwable root = nested.getRootCause(); |
59 |
| - if (root.getMessage().contains("Connection refused")) { |
60 |
| - return true; |
61 |
| - } |
| 53 | + static class SkipOnWindows implements TestRule { |
| 54 | + |
| 55 | + @Override |
| 56 | + public Statement apply(final Statement base, Description description) { |
| 57 | + return new Statement() { |
| 58 | + |
| 59 | + @Override |
| 60 | + public void evaluate() throws Throwable { |
| 61 | + if (!runningOnWindows()) { |
| 62 | + base.evaluate(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private boolean runningOnWindows() { |
| 67 | + return File.separatorChar == '\\'; |
| 68 | + } |
| 69 | + |
| 70 | + }; |
62 | 71 | }
|
63 |
| - return false; |
| 72 | + |
64 | 73 | }
|
65 | 74 |
|
66 | 75 | }
|
0 commit comments