Add nullability annotations to tests in module/spring-boot-mongodb

See gh-47263
This commit is contained in:
Moritz Halbritter
2025-10-13 13:44:08 +02:00
parent a284dec0c1
commit c06f2750f7
11 changed files with 72 additions and 21 deletions
+10
View File
@@ -53,6 +53,16 @@ dependencies {
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.data:spring-data-mongodb")
testCompileOnly("com.google.code.findbugs:jsr305")
testRuntimeOnly("ch.qos.logback:logback-classic")
testRuntimeOnly("io.netty:netty-handler")
}
tasks.named("compileTestJava") {
options.nullability.checking = "tests"
}
tasks.named("compileDockerTestJava") {
options.nullability.checking = "tests"
}
@@ -17,6 +17,7 @@
package org.springframework.boot.mongodb.docker.compose;
import com.mongodb.ConnectionString;
import com.mongodb.MongoCredential;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
import org.springframework.boot.mongodb.autoconfigure.MongoConnectionDetails;
@@ -41,9 +42,11 @@ class MongoDockerComposeConnectionDetailsFactoryIntegrationTests {
private void assertConnectionDetailsWithDatabase(MongoConnectionDetails connectionDetails, String database) {
ConnectionString connectionString = connectionDetails.getConnectionString();
assertThat(connectionString.getCredential().getUserName()).isEqualTo("root");
assertThat(connectionString.getCredential().getPassword()).isEqualTo("secret".toCharArray());
assertThat(connectionString.getCredential().getSource()).isEqualTo("admin");
MongoCredential credential = connectionString.getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("root");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("admin");
assertThat(connectionString.getDatabase()).isEqualTo(database);
}
@@ -24,6 +24,7 @@ import com.mongodb.ServerApiVersion;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
@@ -70,7 +71,7 @@ class MongoHealthIndicatorIntegrationTests {
return mongoHealth(null);
}
private Health mongoHealth(ServerApi serverApi) {
private Health mongoHealth(@Nullable ServerApi serverApi) {
Builder settingsBuilder = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(mongo.getConnectionString()));
if (serverApi != null) {
@@ -79,7 +80,9 @@ class MongoHealthIndicatorIntegrationTests {
MongoClientSettings settings = settingsBuilder.build();
MongoClient mongoClient = MongoClients.create(settings);
MongoHealthIndicator healthIndicator = new MongoHealthIndicator(mongoClient);
return healthIndicator.health(true);
Health health = healthIndicator.health(true);
assertThat(health).isNotNull();
return health;
}
}
@@ -26,6 +26,7 @@ import com.mongodb.ServerApiVersion;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Container;
@@ -64,7 +65,7 @@ class MongoReactiveHealthIndicatorIntegrationTests {
return mongoHealth(null);
}
private Health mongoHealth(ServerApi serverApi) {
private Health mongoHealth(@Nullable ServerApi serverApi) {
Builder settingsBuilder = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(mongo.getConnectionString()));
if (serverApi != null) {
@@ -73,7 +74,9 @@ class MongoReactiveHealthIndicatorIntegrationTests {
MongoClientSettings settings = settingsBuilder.build();
MongoClient mongoClient = MongoClients.create(settings);
MongoReactiveHealthIndicator healthIndicator = new MongoReactiveHealthIndicator(mongoClient);
return healthIndicator.health(true).block(Duration.ofSeconds(30));
Health health = healthIndicator.health(true).block(Duration.ofSeconds(30));
assertThat(health).isNotNull();
return health;
}
private void assertHealth(Health health) {
@@ -139,6 +139,7 @@ class MongoAutoConfigurationTests {
this.contextRunner.withPropertyValues("spring.mongodb.username=user", "spring.mongodb.password=secret")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("test");
@@ -152,6 +153,7 @@ class MongoAutoConfigurationTests {
"spring.mongodb.database=mydb")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("mydb");
@@ -165,6 +167,7 @@ class MongoAutoConfigurationTests {
"spring.mongodb.database=mydb", "spring.mongodb.authentication-database=authdb")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("authdb");
@@ -176,6 +179,7 @@ class MongoAutoConfigurationTests {
this.contextRunner.withPropertyValues("spring.mongodb.username=us:er", "spring.mongodb.password=sec@ret")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("us:er");
assertThat(credential.getPassword()).isEqualTo("sec@ret".toCharArray());
assertThat(credential.getSource()).isEqualTo("test");
@@ -192,6 +196,7 @@ class MongoAutoConfigurationTests {
void configuresCredentialsFromUriPropertyWithDefaultDatabase() {
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://user:secret@localhost/").run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("admin");
@@ -205,6 +210,7 @@ class MongoAutoConfigurationTests {
"spring.mongodb.database=notused", "spring.mongodb.authentication-database=notused")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("mydb");
@@ -218,6 +224,7 @@ class MongoAutoConfigurationTests {
"spring.mongodb.database=notused", "spring.mongodb.authentication-database=notused")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("authdb");
@@ -21,6 +21,7 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
import com.mongodb.MongoClientSettings;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -97,7 +98,7 @@ abstract class MongoClientFactorySupportTests<T> {
MongoClientSettings.builder().build());
}
protected abstract T createMongoClient(List<MongoClientSettingsBuilderCustomizer> customizers,
protected abstract T createMongoClient(@Nullable List<MongoClientSettingsBuilderCustomizer> customizers,
MongoClientSettings settings);
protected abstract MongoClientSettings getClientSettings(T client);
@@ -21,6 +21,7 @@ import java.util.List;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.internal.MongoClientImpl;
import org.jspecify.annotations.Nullable;
/**
* Tests for {@link MongoClientFactory}.
@@ -34,7 +35,7 @@ import com.mongodb.client.internal.MongoClientImpl;
class MongoClientFactoryTests extends MongoClientFactorySupportTests<MongoClient> {
@Override
protected MongoClient createMongoClient(List<MongoClientSettingsBuilderCustomizer> customizers,
protected MongoClient createMongoClient(@Nullable List<MongoClientSettingsBuilderCustomizer> customizers,
MongoClientSettings settings) {
return new MongoClientFactory(customizers).createMongoClient(settings);
}
@@ -130,6 +130,7 @@ class MongoReactiveAutoConfigurationTests {
this.contextRunner.withPropertyValues("spring.mongodb.username=user", "spring.mongodb.password=secret")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("test");
@@ -143,6 +144,7 @@ class MongoReactiveAutoConfigurationTests {
"spring.mongodb.database=mydb")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("mydb");
@@ -156,6 +158,7 @@ class MongoReactiveAutoConfigurationTests {
"spring.mongodb.database=mydb", "spring.mongodb.authentication-database=authdb")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("authdb");
@@ -172,6 +175,7 @@ class MongoReactiveAutoConfigurationTests {
void configuresCredentialsFromUriPropertyWithDefaultDatabase() {
this.contextRunner.withPropertyValues("spring.mongodb.uri=mongodb://user:secret@localhost/").run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("admin");
@@ -185,6 +189,7 @@ class MongoReactiveAutoConfigurationTests {
"spring.mongodb.database=notused", "spring.mongodb.authentication-database=notused")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("mydb");
@@ -198,6 +203,7 @@ class MongoReactiveAutoConfigurationTests {
"spring.mongodb.database=notused", "spring.mongodb.authentication-database=notused")
.run((context) -> {
MongoCredential credential = getSettings(context).getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("authdb");
@@ -212,10 +218,13 @@ class MongoReactiveAutoConfigurationTests {
TransportSettings transportSettings = getSettings(context).getTransportSettings();
assertThat(transportSettings).isInstanceOf(NettyTransportSettings.class);
EventLoopGroup eventLoopGroup = ((NettyTransportSettings) transportSettings).getEventLoopGroup();
assertThat(eventLoopGroup).isNotNull();
assertThat(eventLoopGroup.isShutdown()).isFalse();
eventLoopGroupReference.set(eventLoopGroup);
});
assertThat(eventLoopGroupReference.get().isShutdown()).isTrue();
EventLoopGroup eventLoopGroup = eventLoopGroupReference.get();
assertThat(eventLoopGroup).isNotNull();
assertThat(eventLoopGroup.isShutdown()).isTrue();
}
@Test
@@ -19,6 +19,7 @@ package org.springframework.boot.mongodb.autoconfigure;
import java.util.List;
import com.mongodb.ConnectionString;
import com.mongodb.MongoCredential;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -56,9 +57,11 @@ class PropertiesMongoConnectionDetailsTests {
ConnectionString connectionString = this.connectionDetails.getConnectionString();
assertThat(connectionString.getUsername()).isEqualTo("user");
assertThat(connectionString.getPassword()).isEmpty();
assertThat(connectionString.getCredential().getUserName()).isEqualTo("user");
assertThat(connectionString.getCredential().getPassword()).isEmpty();
assertThat(connectionString.getCredential().getSource()).isEqualTo("test");
MongoCredential credential = connectionString.getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEmpty();
assertThat(credential.getSource()).isEqualTo("test");
}
@Test
@@ -68,9 +71,11 @@ class PropertiesMongoConnectionDetailsTests {
ConnectionString connectionString = this.connectionDetails.getConnectionString();
assertThat(connectionString.getUsername()).isEqualTo("user");
assertThat(connectionString.getPassword()).isEqualTo("secret".toCharArray());
assertThat(connectionString.getCredential().getUserName()).isEqualTo("user");
assertThat(connectionString.getCredential().getPassword()).isEqualTo("secret".toCharArray());
assertThat(connectionString.getCredential().getSource()).isEqualTo("test");
MongoCredential credential = connectionString.getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getUserName()).isEqualTo("user");
assertThat(credential.getPassword()).isEqualTo("secret".toCharArray());
assertThat(credential.getSource()).isEqualTo("test");
}
@Test
@@ -100,8 +105,10 @@ class PropertiesMongoConnectionDetailsTests {
this.properties.setAuthenticationDatabase("authdb");
ConnectionString connectionString = this.connectionDetails.getConnectionString();
assertThat(connectionString.getDatabase()).isEqualTo("db");
assertThat(connectionString.getCredential().getSource()).isEqualTo("authdb");
assertThat(connectionString.getCredential().getUserName()).isEqualTo("user");
MongoCredential credential = connectionString.getCredential();
assertThat(credential).isNotNull();
assertThat(credential.getSource()).isEqualTo("authdb");
assertThat(credential.getUserName()).isEqualTo("user");
}
@Test
@@ -21,6 +21,7 @@ import java.util.List;
import com.mongodb.MongoClientSettings;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.internal.MongoClientImpl;
import org.jspecify.annotations.Nullable;
/**
* Tests for {@link ReactiveMongoClientFactory}.
@@ -32,7 +33,7 @@ import com.mongodb.reactivestreams.client.internal.MongoClientImpl;
class ReactiveMongoClientFactoryTests extends MongoClientFactorySupportTests<MongoClient> {
@Override
protected MongoClient createMongoClient(List<MongoClientSettingsBuilderCustomizer> customizers,
protected MongoClient createMongoClient(@Nullable List<MongoClientSettingsBuilderCustomizer> customizers,
MongoClientSettings settings) {
return new ReactiveMongoClientFactory(customizers).createMongoClient(settings);
}
@@ -197,13 +197,19 @@ class MongoMetricsAutoConfigurationTests {
private MongoCommandTagsProvider getMongoCommandTagsProviderUsedToConstructListener(
final AssertableApplicationContext context) {
MongoMetricsCommandListener listener = context.getBean(MongoMetricsCommandListener.class);
return (MongoCommandTagsProvider) ReflectionTestUtils.getField(listener, "tagsProvider");
MongoCommandTagsProvider tagsProvider = (MongoCommandTagsProvider) ReflectionTestUtils.getField(listener,
"tagsProvider");
assertThat(tagsProvider).isNotNull();
return tagsProvider;
}
private MongoConnectionPoolTagsProvider getMongoConnectionPoolTagsProviderUsedToConstructListener(
final AssertableApplicationContext context) {
MongoMetricsConnectionPoolListener listener = context.getBean(MongoMetricsConnectionPoolListener.class);
return (MongoConnectionPoolTagsProvider) ReflectionTestUtils.getField(listener, "tagsProvider");
MongoConnectionPoolTagsProvider tagsProvider = (MongoConnectionPoolTagsProvider) ReflectionTestUtils
.getField(listener, "tagsProvider");
assertThat(tagsProvider).isNotNull();
return tagsProvider;
}
}