2

We are using spring boot version 1.5.2.RELEASE and Spring Cloud Sleuth version 1.1.2.RELEASE

I am Autowiring Tracer in my service class.

@Autowired
Tracer tracer

When running the application then everything is working fine. Tracer dependency gets injected properly

But when I am running spring mvc junit test cases then unit test cases are failing

Exception :

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cloud.sleuth.Tracer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)

I have searched for it but haven't found any relevant answers.

Kindly help.

following are the cloud dependecies:

    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>

   <dependencyManagement>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
  </dependencyManagement>

PS: I cannot upgrade the spring boot version because of some client limitations.

GAURAV PANT
  • 135
  • 4
  • 11

1 Answers1

1

As you mention Spring MVC JUnit test I assume you are using @WebMvcTest, this will populate a Spring Context for you with only relevant web-layer related parts.

 * Using this annotation will disable full auto-configuration and instead apply only
 * configuration relevant to MVC tests (i.e. {@code @Controller},
 * {@code @ControllerAdvice}, {@code @JsonComponent},
 * {@code Converter}/{@code GenericConverter}, {@code Filter}, {@code WebMvcConfigurer}
 * and {@code HandlerMethodArgumentResolver} beans but not {@code @Component},
 * {@code @Service} or {@code @Repository} beans).

That's why your Tracer is not present in the context you use for the Spring MVC test. Either use @MockBean to mock it or provide an additional e.g @TestConfiguration where you expose this bean by constructing it on your own.

rieckpil
  • 10,470
  • 3
  • 32
  • 56