diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java index f15678eab6..14d94a0e45 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java @@ -22,12 +22,15 @@ import java.util.List; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; +import com.mongodb.connection.Cluster; +import com.mongodb.connection.ClusterSettings; import org.junit.Test; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -55,7 +58,7 @@ public class MongoPropertiesTests { MongoProperties properties = new MongoProperties(); properties.setPort(12345); MongoClient client = properties.createMongoClient(null, null); - List allAddresses = client.getAllAddress(); + List allAddresses = extractServerAddresses(client); assertThat(allAddresses).hasSize(1); assertServerAddress(allAddresses.get(0), "localhost", 12345); } @@ -65,7 +68,7 @@ public class MongoPropertiesTests { MongoProperties properties = new MongoProperties(); properties.setHost("mongo.example.com"); MongoClient client = properties.createMongoClient(null, null); - List allAddresses = client.getAllAddress(); + List allAddresses = extractServerAddresses(client); assertThat(allAddresses).hasSize(1); assertServerAddress(allAddresses.get(0), "mongo.example.com", 27017); } @@ -108,7 +111,7 @@ public class MongoPropertiesTests { properties.setUri("mongodb://user:secret@mongo1.example.com:12345," + "mongo2.example.com:23456/test"); MongoClient client = properties.createMongoClient(null, null); - List allAddresses = client.getAllAddress(); + List allAddresses = extractServerAddresses(client); assertThat(allAddresses).hasSize(2); assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345); assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456); @@ -117,6 +120,14 @@ public class MongoPropertiesTests { assertMongoCredential(credentialsList.get(0), "user", "secret", "test"); } + private List extractServerAddresses(MongoClient client) { + Cluster cluster = (Cluster) ReflectionTestUtils.getField(client, "cluster"); + ClusterSettings clusterSettings = (ClusterSettings) ReflectionTestUtils + .getField(cluster, "settings"); + List allAddresses = clusterSettings.getHosts(); + return allAddresses; + } + private void assertServerAddress(ServerAddress serverAddress, String expectedHost, int expectedPort) { assertThat(serverAddress.getHost()).isEqualTo(expectedHost);