Skip to content

Commit d624c8d

Browse files
golonzovskyodrotbohm
authored andcommitted
spring-projects#110 - Simplified RequiresMongoDB test rule.
We now extend ExternalResource instead of implementing TestRule to avoid the usage of the deprecated AssumptionViolatedException.
1 parent 7f8a0cc commit d624c8d

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

mongodb/util/src/main/java/example/springdata/mongodb/util/RequiresMongoDB.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,12 +17,11 @@
1717

1818
import java.net.UnknownHostException;
1919

20+
import org.junit.AssumptionViolatedException;
2021
import org.junit.ClassRule;
2122
import org.junit.Rule;
22-
import org.junit.internal.AssumptionViolatedException;
23+
import org.junit.rules.ExternalResource;
2324
import org.junit.rules.TestRule;
24-
import org.junit.runner.Description;
25-
import org.junit.runners.model.Statement;
2625
import org.springframework.data.util.Version;
2726
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2827

@@ -33,13 +32,14 @@
3332

3433
/**
3534
* {@link TestRule} verifying server tests are executed against match a given version. This one can be used as
36-
* {@link ClassRule} eg. in context depending tests run with {@link SpringJUnit4ClassRunner} when the context would fail
37-
* to start in case of invalid version, or as simple {@link Rule} on specific tests.
35+
* {@link ClassRule} e.g. in context depending tests run with {@link SpringJUnit4ClassRunner} when the context would
36+
* fail to start in case of invalid version, or as simple {@link Rule} on specific tests.
3837
*
3938
* @author Christoph Strobl
39+
* @author Alexander Golonzovsky
4040
* @since 1.6
4141
*/
42-
public class RequiresMongoDB implements TestRule {
42+
public class RequiresMongoDB extends ExternalResource {
4343

4444
private String host = "localhost";
4545
private int port = 27017;
@@ -74,38 +74,26 @@ public RequiresMongoDB serverRunningAt(String host, int port) {
7474
}
7575

7676
@Override
77-
public Statement apply(final Statement base, Description description) {
77+
protected void before() throws Throwable {
7878

7979
initCurrentVersion();
80-
return new Statement() {
81-
82-
@Override
83-
public void evaluate() throws Throwable {
84-
if (currentVersion != null) {
85-
if (currentVersion.isLessThan(minVersion) || currentVersion.isGreaterThan(maxVersion)) {
86-
throw new AssumptionViolatedException(String.format(
87-
"Expected mongodb server to be in range %s to %s but found %s", minVersion, maxVersion, currentVersion));
88-
}
89-
}
90-
base.evaluate();
91-
}
92-
};
80+
81+
if (currentVersion.isLessThan(minVersion) || currentVersion.isGreaterThan(maxVersion)) {
82+
throw new AssumptionViolatedException(String.format(
83+
"Expected mongodb server to be in range %s to %s but found %s", minVersion, maxVersion, currentVersion));
84+
}
9385
}
9486

9587
private void initCurrentVersion() {
9688

9789
if (currentVersion == null) {
9890
try {
99-
MongoClient client;
100-
client = new MongoClient(host, port);
101-
DB db = client.getDB("test");
91+
DB db = new MongoClient(host, port).getDB("test");
10292
CommandResult result = db.command(new BasicDBObjectBuilder().add("buildInfo", 1).get());
10393
this.currentVersion = Version.parse(result.get("version").toString());
10494
} catch (com.mongodb.MongoTimeoutException | UnknownHostException e) {
10595
throw new AssumptionViolatedException("Seems as mongodb server is not running.", e);
10696
}
10797
}
108-
10998
}
110-
11199
}

0 commit comments

Comments
 (0)