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.
This commit is contained in:
parent
ae89cb0355
commit
275651e89a
|
|
@ -16,11 +16,16 @@
|
||||||
|
|
||||||
package sample.data.cassandra;
|
package sample.data.cassandra;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
import org.cassandraunit.spring.CassandraDataSet;
|
import org.cassandraunit.spring.CassandraDataSet;
|
||||||
import org.cassandraunit.spring.EmbeddedCassandra;
|
import org.cassandraunit.spring.EmbeddedCassandra;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TestRule;
|
||||||
|
import org.junit.runner.Description;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.model.Statement;
|
||||||
|
|
||||||
import org.springframework.boot.test.IntegrationTest;
|
import org.springframework.boot.test.IntegrationTest;
|
||||||
import org.springframework.boot.test.IntegrationTestPropertiesListener;
|
import org.springframework.boot.test.IntegrationTestPropertiesListener;
|
||||||
|
|
@ -48,6 +53,9 @@ public class SampleCassandraApplicationTests {
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static OutputCapture outputCapture = new OutputCapture();
|
public static OutputCapture outputCapture = new OutputCapture();
|
||||||
|
|
||||||
|
@ClassRule
|
||||||
|
public static SkipOnWindows skipOnWindows = new SkipOnWindows();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultSettings() throws Exception {
|
public void testDefaultSettings() throws Exception {
|
||||||
String output = SampleCassandraApplicationTests.outputCapture.toString();
|
String output = SampleCassandraApplicationTests.outputCapture.toString();
|
||||||
|
|
@ -55,4 +63,26 @@ public class SampleCassandraApplicationTests {
|
||||||
output.contains("firstName='Alice', lastName='Smith'"));
|
output.contains("firstName='Alice', lastName='Smith'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class SkipOnWindows implements TestRule {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Statement apply(final Statement base, Description description) {
|
||||||
|
return new Statement() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void evaluate() throws Throwable {
|
||||||
|
if (!runningOnWindows()) {
|
||||||
|
base.evaluate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean runningOnWindows() {
|
||||||
|
return File.separatorChar == '\\';
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,17 @@
|
||||||
|
|
||||||
package sample.data.elasticsearch;
|
package sample.data.elasticsearch;
|
||||||
|
|
||||||
import java.net.ConnectException;
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.junit.ClassRule;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TestRule;
|
||||||
|
import org.junit.runner.Description;
|
||||||
|
import org.junit.runners.model.Statement;
|
||||||
|
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
import org.springframework.boot.test.OutputCapture;
|
import org.springframework.boot.test.OutputCapture;
|
||||||
import org.springframework.core.NestedCheckedException;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
|
@ -41,33 +44,38 @@ public class SampleElasticsearchApplicationTests {
|
||||||
@Rule
|
@Rule
|
||||||
public OutputCapture outputCapture = new OutputCapture();
|
public OutputCapture outputCapture = new OutputCapture();
|
||||||
|
|
||||||
|
@ClassRule
|
||||||
|
public static SkipOnWindows skipOnWindows = new SkipOnWindows();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultSettings() throws Exception {
|
public void testDefaultSettings() throws Exception {
|
||||||
try {
|
|
||||||
new SpringApplicationBuilder(SampleElasticsearchApplication.class)
|
new SpringApplicationBuilder(SampleElasticsearchApplication.class)
|
||||||
.properties(PROPERTIES).run();
|
.properties(PROPERTIES).run();
|
||||||
}
|
|
||||||
catch (IllegalStateException ex) {
|
|
||||||
if (serverNotRunning(ex)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String output = this.outputCapture.toString();
|
String output = this.outputCapture.toString();
|
||||||
assertTrue("Wrong output: " + output,
|
assertTrue("Wrong output: " + output,
|
||||||
output.contains("firstName='Alice', lastName='Smith'"));
|
output.contains("firstName='Alice', lastName='Smith'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean serverNotRunning(IllegalStateException ex) {
|
static class SkipOnWindows implements TestRule {
|
||||||
@SuppressWarnings("serial")
|
|
||||||
NestedCheckedException nested = new NestedCheckedException("failed", ex) {
|
@Override
|
||||||
|
public Statement apply(final Statement base, Description description) {
|
||||||
|
return new Statement() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void evaluate() throws Throwable {
|
||||||
|
if (!runningOnWindows()) {
|
||||||
|
base.evaluate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean runningOnWindows() {
|
||||||
|
return File.separatorChar == '\\';
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
if (nested.contains(ConnectException.class)) {
|
|
||||||
Throwable root = nested.getRootCause();
|
|
||||||
if (root.getMessage().contains("Connection refused")) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue