Spring Sleuth Tracer的自动装配在Spring MVC测试中不起作用。

huangapple go评论98阅读模式
英文:

Autowiring of Spring sleuth Tracer not working in Spring MVC Test

问题

我们正在使用Spring Boot版本1.5.2.RELEASE和Spring Cloud Sleuth版本1.1.2.RELEASE。

在我的服务类中,我正在自动装配Tracer。

@Autowired
Tracer tracer

当运行应用程序时,一切都正常工作。Tracer依赖项被正确注入。

但是,当运行Spring MVC JUnit测试用例时,单元测试用例失败。

异常:

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)}

我已经搜索过,但没有找到相关的答案。

请帮助。

以下是云依赖项:

<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:由于某些客户端限制,我无法升级Spring Boot版本。

英文:

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:

	&lt;dependency&gt;
			&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
			&lt;artifactId&gt;spring-cloud-starter-sleuth&lt;/artifactId&gt;
	&lt;/dependency&gt;

   &lt;dependencyManagement&gt;
			&lt;dependency&gt;
				&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
				&lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
				&lt;version&gt;Camden.SR5&lt;/version&gt;
				&lt;type&gt;pom&lt;/type&gt;
				&lt;scope&gt;import&lt;/scope&gt;
			&lt;/dependency&gt;
  &lt;/dependencyManagement&gt;

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

答案1

得分: 1

正如你提到的Spring MVC JUnit测试,我假设你正在使用@WebMvcTest,这将为你创建一个Spring上下文,其中仅包含与Web层相关的部分。

  • 使用此注解将禁用完整的自动配置,而是仅应用与MVC测试相关的配置(即@Controller@ControllerAdvice@JsonComponentConverter/GenericConverterFilterWebMvcConfigurerHandlerMethodArgumentResolver beans,但不包括@Component@Service@Repository beans)。

这就是为什么你的Tracer在你用于Spring MVC测试的上下文中不可用的原因。要么使用@MockBean来模拟它,要么提供一个额外的@TestConfiguration,在其中通过自己构建来公开此bean。

英文:

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.

huangapple
  • 本文由 发表于 2020年7月22日 18:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63032211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定