Skip to content

Commit

Permalink
improve tests for RestTemplate (#11053)
Browse files Browse the repository at this point in the history
Co-authored-by: Lauri Tulmin <tulmin@gmail.com>
  • Loading branch information
zeitlinger and laurit authored Apr 11, 2024
1 parent 0026dda commit 2722582
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 73 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,47 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.web.client.RestTemplate;

class SpringWebInstrumentationAutoConfigurationTest {

private final ApplicationContextRunner contextRunner =
new ApplicationContextRunner()
.withBean(OpenTelemetry.class, OpenTelemetry::noop)
.withBean(RestTemplate.class, RestTemplate::new)
.withConfiguration(
AutoConfigurations.of(SpringWebInstrumentationAutoConfiguration.class));

/**
* Tests that users create {@link RestTemplate} bean is instrumented.
*
* <pre>{@code
* @Bean public RestTemplate restTemplate() {
* return new RestTemplate();
* }
* }</pre>
*/
@Test
void instrumentationEnabled() {
contextRunner
.withPropertyValues("otel.instrumentation.spring-web.enabled=true")
.run(
context ->
assertThat(
context.getBean(
"otelRestTemplateBeanPostProcessor",
RestTemplateBeanPostProcessor.class))
.isNotNull());
context -> {
assertThat(
context.getBean(
"otelRestTemplateBeanPostProcessor", RestTemplateBeanPostProcessor.class))
.isNotNull();

assertThat(
context.getBean(RestTemplate.class).getInterceptors().stream()
.filter(
rti ->
rti.getClass()
.getName()
.startsWith("io.opentelemetry.instrumentation"))
.count())
.isEqualTo(1);
});
}

@Test
Expand Down

0 comments on commit 2722582

Please sign in to comment.