Skip to content

Commit 275651e

Browse files
committed
Skips Cassandra and Elasticsearch tests on Windows
Neither Cassandra nor Elasticsearch starts reliably on Windows. This commit adds a custom class rule to the associated sample application tests to skip them on Windows. A class rule is used rather than a Unit assumption as we want to avoid starting Elasticsearch (done by the application context) and Cassandra (done by a test execution listener) and an assumption would be too late.
1 parent ae89cb0 commit 275651e

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

spring-boot-samples/spring-boot-sample-data-cassandra/src/test/java/sample/data/cassandra/SampleCassandraApplicationTests.java

+30
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@
1616

1717
package sample.data.cassandra;
1818

19+
import java.io.File;
20+
1921
import org.cassandraunit.spring.CassandraDataSet;
2022
import org.cassandraunit.spring.EmbeddedCassandra;
2123
import org.junit.ClassRule;
2224
import org.junit.Test;
25+
import org.junit.rules.TestRule;
26+
import org.junit.runner.Description;
2327
import org.junit.runner.RunWith;
28+
import org.junit.runners.model.Statement;
2429

2530
import org.springframework.boot.test.IntegrationTest;
2631
import org.springframework.boot.test.IntegrationTestPropertiesListener;
@@ -48,11 +53,36 @@ public class SampleCassandraApplicationTests {
4853
@ClassRule
4954
public static OutputCapture outputCapture = new OutputCapture();
5055

56+
@ClassRule
57+
public static SkipOnWindows skipOnWindows = new SkipOnWindows();
58+
5159
@Test
5260
public void testDefaultSettings() throws Exception {
5361
String output = SampleCassandraApplicationTests.outputCapture.toString();
5462
assertTrue("Wrong output: " + output,
5563
output.contains("firstName='Alice', lastName='Smith'"));
5664
}
5765

66+
static class SkipOnWindows implements TestRule {
67+
68+
@Override
69+
public Statement apply(final Statement base, Description description) {
70+
return new Statement() {
71+
72+
@Override
73+
public void evaluate() throws Throwable {
74+
if (!runningOnWindows()) {
75+
base.evaluate();
76+
}
77+
}
78+
79+
private boolean runningOnWindows() {
80+
return File.separatorChar == '\\';
81+
}
82+
83+
};
84+
}
85+
86+
}
87+
5888
}

spring-boot-samples/spring-boot-sample-data-elasticsearch/src/test/java/sample/data/elasticsearch/SampleElasticsearchApplicationTests.java

+29-21
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616

1717
package sample.data.elasticsearch;
1818

19-
import java.net.ConnectException;
19+
import java.io.File;
2020

21+
import org.junit.ClassRule;
2122
import org.junit.Rule;
2223
import org.junit.Test;
24+
import org.junit.rules.TestRule;
25+
import org.junit.runner.Description;
26+
import org.junit.runners.model.Statement;
2327

2428
import org.springframework.boot.builder.SpringApplicationBuilder;
2529
import org.springframework.boot.test.OutputCapture;
26-
import org.springframework.core.NestedCheckedException;
2730

2831
import static org.junit.Assert.assertTrue;
2932

@@ -41,33 +44,38 @@ public class SampleElasticsearchApplicationTests {
4144
@Rule
4245
public OutputCapture outputCapture = new OutputCapture();
4346

47+
@ClassRule
48+
public static SkipOnWindows skipOnWindows = new SkipOnWindows();
49+
4450
@Test
4551
public void testDefaultSettings() throws Exception {
46-
try {
47-
new SpringApplicationBuilder(SampleElasticsearchApplication.class)
48-
.properties(PROPERTIES).run();
49-
}
50-
catch (IllegalStateException ex) {
51-
if (serverNotRunning(ex)) {
52-
return;
53-
}
54-
}
52+
new SpringApplicationBuilder(SampleElasticsearchApplication.class)
53+
.properties(PROPERTIES).run();
5554
String output = this.outputCapture.toString();
5655
assertTrue("Wrong output: " + output,
5756
output.contains("firstName='Alice', lastName='Smith'"));
5857
}
5958

60-
private boolean serverNotRunning(IllegalStateException ex) {
61-
@SuppressWarnings("serial")
62-
NestedCheckedException nested = new NestedCheckedException("failed", ex) {
63-
};
64-
if (nested.contains(ConnectException.class)) {
65-
Throwable root = nested.getRootCause();
66-
if (root.getMessage().contains("Connection refused")) {
67-
return true;
68-
}
59+
static class SkipOnWindows implements TestRule {
60+
61+
@Override
62+
public Statement apply(final Statement base, Description description) {
63+
return new Statement() {
64+
65+
@Override
66+
public void evaluate() throws Throwable {
67+
if (!runningOnWindows()) {
68+
base.evaluate();
69+
}
70+
}
71+
72+
private boolean runningOnWindows() {
73+
return File.separatorChar == '\\';
74+
}
75+
76+
};
6977
}
70-
return false;
78+
7179
}
7280

7381
}

0 commit comments

Comments
 (0)