Browse Source

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

See gh-47263
pull/47637/head
Moritz Halbritter 2 months ago
parent
commit
6477657e43
  1. 4
      module/spring-boot-sendgrid/build.gradle
  2. 30
      module/spring-boot-sendgrid/src/test/java/org/springframework/boot/sendgrid/autoconfigure/SendGridAutoConfigurationTests.java

4
module/spring-boot-sendgrid/build.gradle

@ -35,3 +35,7 @@ dependencies {
testRuntimeOnly("ch.qos.logback:logback-classic") testRuntimeOnly("ch.qos.logback:logback-classic")
} }
tasks.named("compileTestJava") {
options.nullability.checking = "tests"
}

30
module/spring-boot-sendgrid/src/test/java/org/springframework/boot/sendgrid/autoconfigure/SendGridAutoConfigurationTests.java

@ -18,6 +18,7 @@ package org.springframework.boot.sendgrid.autoconfigure;
import com.sendgrid.SendGrid; import com.sendgrid.SendGrid;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner; import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -39,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/ */
class SendGridAutoConfigurationTests { class SendGridAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private @Nullable AnnotationConfigApplicationContext context;
@AfterEach @AfterEach
void close() { void close() {
@ -50,38 +51,40 @@ class SendGridAutoConfigurationTests {
@Test @Test
void expectedSendGridBeanCreatedApiKey() { void expectedSendGridBeanCreatedApiKey() {
loadContext("spring.sendgrid.api-key:SG.SECRET-API-KEY"); AnnotationConfigApplicationContext context = loadContext("spring.sendgrid.api-key:SG.SECRET-API-KEY");
SendGrid sendGrid = this.context.getBean(SendGrid.class); SendGrid sendGrid = context.getBean(SendGrid.class);
assertThat(sendGrid.getRequestHeaders()).containsEntry("Authorization", "Bearer SG.SECRET-API-KEY"); assertThat(sendGrid.getRequestHeaders()).containsEntry("Authorization", "Bearer SG.SECRET-API-KEY");
} }
@Test @Test
void autoConfigurationNotFiredWhenPropertiesNotSet() { void autoConfigurationNotFiredWhenPropertiesNotSet() {
loadContext(); AnnotationConfigApplicationContext context = loadContext();
assertThatExceptionOfType(NoSuchBeanDefinitionException.class) assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> this.context.getBean(SendGrid.class)); .isThrownBy(() -> context.getBean(SendGrid.class));
} }
@Test @Test
void autoConfigurationNotFiredWhenBeanAlreadyCreated() { void autoConfigurationNotFiredWhenBeanAlreadyCreated() {
loadContext(ManualSendGridConfiguration.class, "spring.sendgrid.api-key:SG.SECRET-API-KEY"); AnnotationConfigApplicationContext context = loadContext(ManualSendGridConfiguration.class,
SendGrid sendGrid = this.context.getBean(SendGrid.class); "spring.sendgrid.api-key:SG.SECRET-API-KEY");
SendGrid sendGrid = context.getBean(SendGrid.class);
assertThat(sendGrid.getRequestHeaders()).containsEntry("Authorization", "Bearer SG.CUSTOM_API_KEY"); assertThat(sendGrid.getRequestHeaders()).containsEntry("Authorization", "Bearer SG.CUSTOM_API_KEY");
} }
@Test @Test
void expectedSendGridBeanWithProxyCreated() { void expectedSendGridBeanWithProxyCreated() {
loadContext("spring.sendgrid.api-key:SG.SECRET-API-KEY", "spring.sendgrid.proxy.host:localhost", AnnotationConfigApplicationContext context = loadContext("spring.sendgrid.api-key:SG.SECRET-API-KEY",
"spring.sendgrid.proxy.port:5678"); "spring.sendgrid.proxy.host:localhost", "spring.sendgrid.proxy.port:5678");
SendGrid sendGrid = this.context.getBean(SendGrid.class); SendGrid sendGrid = context.getBean(SendGrid.class);
assertThat(sendGrid).extracting("client.httpClient.routePlanner").isInstanceOf(DefaultProxyRoutePlanner.class); assertThat(sendGrid).extracting("client.httpClient.routePlanner").isInstanceOf(DefaultProxyRoutePlanner.class);
} }
private void loadContext(String... environment) { private AnnotationConfigApplicationContext loadContext(String... environment) {
loadContext(null, environment); return loadContext(null, environment);
} }
private void loadContext(Class<?> additionalConfiguration, String... environment) { private AnnotationConfigApplicationContext loadContext(@Nullable Class<?> additionalConfiguration,
String... environment) {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of(environment).applyTo(this.context); TestPropertyValues.of(environment).applyTo(this.context);
ConfigurationPropertySources.attach(this.context.getEnvironment()); ConfigurationPropertySources.attach(this.context.getEnvironment());
@ -90,6 +93,7 @@ class SendGridAutoConfigurationTests {
this.context.register(additionalConfiguration); this.context.register(additionalConfiguration);
} }
this.context.refresh(); this.context.refresh();
return this.context;
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

Loading…
Cancel
Save