英文:
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:
<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.
答案1
得分: 1
正如你提到的Spring MVC JUnit测试,我假设你正在使用@WebMvcTest
,这将为你创建一个Spring上下文,其中仅包含与Web层相关的部分。
- 使用此注解将禁用完整的自动配置,而是仅应用与MVC测试相关的配置(即
@Controller
、@ControllerAdvice
、@JsonComponent
、Converter
/GenericConverter
、Filter
、WebMvcConfigurer
和HandlerMethodArgumentResolver
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论