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

Loading…
Cancel
Save