Use assertJ in tests (#876)

This commit is contained in:
Munir Abdinur 2020-08-03 15:09:42 -04:00 committed by GitHub
parent 6a61bc824d
commit 49017cf864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 125 additions and 124 deletions

View File

@ -16,12 +16,11 @@
package io.opentelemetry.instrumentation.spring.autoconfigure;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.trace.Tracer;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.TestConfiguration;
@ -29,7 +28,7 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
/** Spring Boot auto configuration test for {@link TracerAutoConfiguration} */
public class TracerAutoConfigurationTest {
class TracerAutoConfigurationTest {
@TestConfiguration
static class CustomTracerConfiguration {
@Bean
@ -41,45 +40,39 @@ public class TracerAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
@Test
public void should_NOT_initalize_otelTracer_if_tracer_bean_exists() {
@DisplayName("when Application Context contains Tracer bean should NOT initalize otelTracer")
void customTracer() {
this.contextRunner
.withUserConfiguration(CustomTracerConfiguration.class)
.withConfiguration(AutoConfigurations.of(TracerAutoConfiguration.class))
.run(
(context) -> {
assertTrue(
"Application Context contains customTestTracer bean",
context.containsBean("customTestTracer"));
assertFalse(
"Application Context DOES NOT contain the otelTracer bean defined in TracerAutoConfiguration",
context.containsBean("otelTracer"));
assertThat(context.containsBean("customTestTracer")).isTrue();
assertThat(context.containsBean("otelTracer")).isFalse();
});
}
@Test
public void should_contain_otelTracer_bean() {
@DisplayName("when Application Context DOES NOT contain Tracer bean should initialize otelTracer")
void initalizeTracer() {
this.contextRunner
.withConfiguration(AutoConfigurations.of(TracerAutoConfiguration.class))
.run(
(context) -> {
assertTrue(
"Application Context contains otelTracer bean",
context.containsBean("otelTracer"));
assertThat(context.containsBean("otelTracer")).isTrue();
});
}
@Test
public void should_set_tracer_name() {
@DisplayName("when opentelemetry.trace.tracer.name is set should initialize tracer with name")
void withTracerNameProperty() {
this.contextRunner
.withPropertyValues("opentelemetry.trace.tracer.name=testTracer")
.withConfiguration(AutoConfigurations.of(TracerAutoConfiguration.class))
.run(
(context) -> {
assertEquals(
"Application Context sets the Tracer with name to testTracer",
context.getBean("otelTracer", Tracer.class),
OpenTelemetry.getTracer("testTracer"));
assertThat(context.getBean("otelTracer", Tracer.class))
.isEqualTo(OpenTelemetry.getTracer("testTracer"));
});
}
}

View File

@ -16,16 +16,16 @@
package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
/** Spring Boot auto configuration test for {@link RestTemplateAutoConfiguration} */
public class RestTemplateAutoConfigurationTest {
class RestTemplateAutoConfigurationTest {
private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
@ -34,39 +34,40 @@ public class RestTemplateAutoConfigurationTest {
TracerAutoConfiguration.class, RestTemplateAutoConfiguration.class));
@Test
public void should_initialize_RestTemplateInterceptor_bean_when_httpclients_are_ENABLED() {
@DisplayName("when httpclients are ENABLED should initialize RestTemplateInterceptor bean")
void httpClientsEnabled() {
this.contextRunner
.withPropertyValues("opentelemetry.trace.httpclients.enabled=true")
.run(
(context) -> {
assertNotNull(
"Application Context contains RestTemplateBeanPostProcessor bean",
context.getBean(
"otelRestTemplateBeanPostProcessor", RestTemplateBeanPostProcessor.class));
assertThat(
context.getBean(
"otelRestTemplateBeanPostProcessor", RestTemplateBeanPostProcessor.class))
.isNotNull();
});
}
@Test
public void should_NOT_initialize_RestTemplateInterceptor_bean_when_httpclients_are_DISABLED() {
@DisplayName("when httpclients are DISABLED should NOT initialize RestTemplateInterceptor bean")
void disabledProperty() {
this.contextRunner
.withPropertyValues("opentelemetry.trace.httpclients.enabled=false")
.run(
(context) -> {
assertFalse(
"Application Context DOES NOT contain otelRestTemplateBeanPostProcessor bean",
context.containsBean("otelRestTemplateBeanPostProcessor"));
assertThat(context.containsBean("otelRestTemplateBeanPostProcessor")).isFalse();
});
}
@Test
public void
should_initialize_RestTemplateInterceptor_bean_when_httpclients_enabled_property_is_MISSING() {
@DisplayName(
"when httpclients enabled property is MISSING should initialize RestTemplateInterceptor bean")
void noProperty() {
this.contextRunner.run(
(context) -> {
assertNotNull(
"Application Context contains RestTemplateBeanPostProcessor bean",
context.getBean(
"otelRestTemplateBeanPostProcessor", RestTemplateBeanPostProcessor.class));
assertThat(
context.getBean(
"otelRestTemplateBeanPostProcessor", RestTemplateBeanPostProcessor.class))
.isNotNull();
});
}
}

View File

@ -16,12 +16,12 @@
package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.resttemplate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.instrumentation.spring.httpclients.RestTemplateInterceptor;
import io.opentelemetry.trace.Tracer;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@ -29,42 +29,43 @@ import org.springframework.web.client.RestTemplate;
/** Spring bean post processor test {@link RestTemplateBeanPostProcessor} */
@RunWith(MockitoJUnitRunner.class)
public class RestTemplateBeanPostProcessorTest {
class RestTemplateBeanPostProcessorTest {
@Mock Tracer tracer;
RestTemplateBeanPostProcessor restTemplateBeanPostProcessor =
new RestTemplateBeanPostProcessor(tracer);
@Test
public void should_return_object_if_processed_bean_is_not_of_type_RestTemplate() {
assertEquals(
restTemplateBeanPostProcessor
.postProcessAfterInitialization(new Object(), "testObject")
.getClass(),
Object.class);
@DisplayName("when processed bean is not of type RestTemplate should return object")
void returnsObject() {
assertThat(
restTemplateBeanPostProcessor.postProcessAfterInitialization(
new Object(), "testObject"))
.isExactlyInstanceOf(Object.class);
}
@Test
public void should_return_RestTemplate_if_processed_bean_is_of_type_RestTemplate() {
assertTrue(
restTemplateBeanPostProcessor.postProcessAfterInitialization(
new RestTemplate(), "testRestTemplate")
instanceof RestTemplate);
@DisplayName("when processed bean is of type RestTemplate should return RestTemplate")
void returnsRestTemplate() {
assertThat(
restTemplateBeanPostProcessor.postProcessAfterInitialization(
new RestTemplate(), "testRestTemplate"))
.isInstanceOf(RestTemplate.class);
}
@Test
public void should_add_ONE_RestTemplateInterceptor_if_processed_bean_is_of_type_RestTemplate() {
@DisplayName("when processed bean is of type RestTemplate should add ONE RestTemplateInterceptor")
void addsRestTemplateInterceptor() {
RestTemplate restTemplate = new RestTemplate();
restTemplateBeanPostProcessor.postProcessAfterInitialization(restTemplate, "testRestTemplate");
restTemplateBeanPostProcessor.postProcessAfterInitialization(restTemplate, "testRestTemplate");
restTemplateBeanPostProcessor.postProcessAfterInitialization(restTemplate, "testRestTemplate");
assertEquals(
restTemplate.getInterceptors().stream()
.filter(rti -> rti instanceof RestTemplateInterceptor)
.count(),
1);
assertThat(
restTemplate.getInterceptors().stream()
.filter(rti -> rti instanceof RestTemplateInterceptor)
.count())
.isEqualTo(1);
}
}

View File

@ -16,16 +16,16 @@
package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
/** Spring Boot auto configuration test for {@link WebClientAutoConfiguration} */
public class WebClientAutoConfigurationTest {
class WebClientAutoConfigurationTest {
private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
@ -34,39 +34,41 @@ public class WebClientAutoConfigurationTest {
TracerAutoConfiguration.class, WebClientAutoConfiguration.class));
@Test
public void should_initialize_WebClientBeanPostProccesor_bean_when_httpclients_are_ENABLED() {
@DisplayName("when httpclients are ENABLED should initialize WebClientBeanPostProccesor bean")
void httpClientsEnabled() {
this.contextRunner
.withPropertyValues("opentelemetry.trace.httpclients.enabled=true")
.run(
(context) -> {
assertNotNull(
"Application Context contains WebClientBeanPostProcessor bean",
context.getBean(
"otelWebClientBeanPostProcessor", WebClientBeanPostProcessor.class));
assertThat(
context.getBean(
"otelWebClientBeanPostProcessor", WebClientBeanPostProcessor.class))
.isNotNull();
});
}
@Test
public void
should_NOT_initialize_WebClientBeanPostProccesor_bean_when_httpclients_are_DISABLED() {
@DisplayName(
"when httpclients are DISABLED should NOT initialize WebClientBeanPostProccesor bean")
void disabledProperty() {
this.contextRunner
.withPropertyValues("opentelemetry.trace.httpclients.enabled=false")
.run(
(context) -> {
assertFalse(
"Application Context DOES NOT contain otelWebClientBeanPostProcessor bean",
context.containsBean("otelWebClientBeanPostProcessor"));
assertThat(context.containsBean("otelWebClientBeanPostProcessor")).isFalse();
});
}
@Test
public void
should_initialize_WebClientBeanPostProccesor_bean_when_httpclients_enabled_property_is_MISSING() {
@DisplayName(
"when httpclients enabled property is MISSING should initialize WebClientBeanPostProccesor bean")
void noProperty() {
this.contextRunner.run(
(context) -> {
assertNotNull(
"Application Context contains WebClientBeanPostProcessor bean",
context.getBean("otelWebClientBeanPostProcessor", WebClientBeanPostProcessor.class));
assertThat(
context.getBean(
"otelWebClientBeanPostProcessor", WebClientBeanPostProcessor.class))
.isNotNull();
});
}
}

View File

@ -16,12 +16,12 @@
package io.opentelemetry.instrumentation.spring.autoconfigure.httpclients.webclient;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.instrumentation.springwebflux.client.WebClientTracingFilter;
import io.opentelemetry.trace.Tracer;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
@ -29,60 +29,64 @@ import org.springframework.web.reactive.function.client.WebClient;
/** Spring bean post processor test {@link WebClientBeanPostProcessor} */
@RunWith(MockitoJUnitRunner.class)
public class WebClientBeanPostProcessorTest {
class WebClientBeanPostProcessorTest {
@Mock Tracer tracer;
WebClientBeanPostProcessor webClientBeanPostProcessor = new WebClientBeanPostProcessor(tracer);
@Test
public void
should_return_object_if_processed_bean_is_NOT_of_type_WebClient_or_WebClientBuilder() {
assertEquals(
webClientBeanPostProcessor
.postProcessAfterInitialization(new Object(), "testObject")
.getClass(),
Object.class);
@DisplayName(
"when processed bean is NOT of type WebClient or WebClientBuilder should return Object")
void returnsObject() {
assertThat(
webClientBeanPostProcessor.postProcessAfterInitialization(new Object(), "testObject"))
.isExactlyInstanceOf(Object.class);
}
@Test
public void should_return_web_client_if_processed_bean_is_of_type_WebClient() {
WebClient webClient = WebClient.create();
assertTrue(
webClientBeanPostProcessor.postProcessAfterInitialization(webClient, "testWebClient")
instanceof WebClient);
@DisplayName("when processed bean is of type WebClient should return WebClient")
void returnsWebClient() {
assertThat(
webClientBeanPostProcessor.postProcessAfterInitialization(
WebClient.create(), "testWebClient"))
.isInstanceOf(WebClient.class);
}
@Test
public void should_return_WebClientBuilder_if_processed_bean_is_of_type_WebClientBuilder() {
WebClient.Builder webClientBuilder = WebClient.builder();
assertTrue(
webClientBeanPostProcessor.postProcessAfterInitialization(
webClientBuilder, "testWebClientBuilder")
instanceof WebClient.Builder);
@DisplayName("when processed bean is of type WebClientBuilder should return WebClientBuilder")
void returnsWebClientBuilder() {
assertThat(
webClientBeanPostProcessor.postProcessAfterInitialization(
WebClient.builder(), "testWebClientBuilder"))
.isInstanceOf(WebClient.Builder.class);
}
@Test
public void should_add_exchange_filter_to_WebClient() {
@DisplayName("when processed bean is of type WebClient should add exchange filter to WebClient")
void addsExchangeFilterWebClient() {
WebClient webClient = WebClient.create();
Object processedWebClient =
webClientBeanPostProcessor.postProcessAfterInitialization(webClient, "testWebClient");
assertTrue(processedWebClient instanceof WebClient);
assertThat(processedWebClient).isInstanceOf(WebClient.class);
((WebClient) processedWebClient)
.mutate()
.filters(
functions -> {
assertEquals(
functions.stream().filter(wctf -> wctf instanceof WebClientTracingFilter).count(),
1);
assertThat(
functions.stream()
.filter(wctf -> wctf instanceof WebClientTracingFilter)
.count())
.isEqualTo(1);
});
}
@Test
public void should_add_ONE_exchange_filter_to_WebClientBuilder() {
@DisplayName(
"when processed bean is of type WebClientBuilder should add ONE exchange filter to WebClientBuilder")
void addsExchangeFilterWebClientBuilder() {
WebClient.Builder webClientBuilder = WebClient.builder();
webClientBeanPostProcessor.postProcessAfterInitialization(
@ -94,8 +98,9 @@ public class WebClientBeanPostProcessorTest {
webClientBuilder.filters(
functions -> {
assertEquals(
functions.stream().filter(wctf -> wctf instanceof WebClientTracingFilter).count(), 1);
assertThat(
functions.stream().filter(wctf -> wctf instanceof WebClientTracingFilter).count())
.isEqualTo(1);
});
}
}

View File

@ -16,17 +16,17 @@
package io.opentelemetry.instrumentation.spring.autoconfigure.webmvc;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.instrumentation.spring.autoconfigure.TracerAutoConfiguration;
import io.opentelemetry.instrumentation.springwebmvc.WebMVCTracingFilter;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
/** Spring Boot auto configuration test for {@link WebMVCFilterAutoConfiguration} */
public class WebMVCFilterAutoConfigurationTest {
class WebMVCFilterAutoConfigurationTest {
private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withConfiguration(
@ -34,36 +34,35 @@ public class WebMVCFilterAutoConfigurationTest {
TracerAutoConfiguration.class, WebMVCFilterAutoConfiguration.class));
@Test
public void should_initialize_WebMvcTracingFilter_bean_when_web_is_enabled() {
@DisplayName("when web is ENABLED should initialize WebMvcTracingFilter bean")
void webEnabled() {
this.contextRunner
.withPropertyValues("opentelemetry.trace.web.enabled=true")
.run(
(context) -> {
assertNotNull(
"Application Context contains WebMVCTracingFilter bean",
context.getBean("otelWebMVCTracingFilter", WebMVCTracingFilter.class));
assertThat(context.getBean("otelWebMVCTracingFilter", WebMVCTracingFilter.class))
.isNotNull();
});
}
@Test
public void should_NOT_initialize_WebMvcTracingFilter_bean_when_web_is_NOT_enabled() {
@DisplayName("when web is DISABLED should NOT initialize WebMvcTracingFilter bean")
void disabledProperty() {
this.contextRunner
.withPropertyValues("opentelemetry.trace.web.enabled=false")
.run(
(context) -> {
assertFalse(
"Application Context DOES NOT contain WebMVCTracingFilter bean",
context.containsBean("otelWebMVCTracingFilter"));
assertThat(context.containsBean("otelWebMVCTracingFilter")).isFalse();
});
}
@Test
public void should_initialize_WebMvcTracingFilter_bean_when_web_enabled_property_is_MISSING() {
@DisplayName("when web property is MISSING should initialize WebMvcTracingFilter bean")
void noProperty() {
this.contextRunner.run(
(context) -> {
assertNotNull(
"Application Context contains WebMVCTracingFilter bean",
context.getBean("otelWebMVCTracingFilter", WebMVCTracingFilter.class));
assertThat(context.getBean("otelWebMVCTracingFilter", WebMVCTracingFilter.class))
.isNotNull();
});
}
}