我创建了一个集成测试来测试我刚刚添加的新功能,但 Spring 接线不起作用。单元测试所有工作,现有的 Spring 集成测试仍然有效,但我无法自动连接我的新类
这是错误消息 -
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.xxx.xxx.etc.MyNewClassTest’: Unsatisfied dependency expressed through field 'sut'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.xxx.xxx.etc.MyNewClass ' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
新班级——
@Slf4j
@Component
public class MyNewClass extends AbstractRetryJob<Event> {
我的测试 -
@ExtendWith(SpringExtension.class)
class MyNewClassTest {
@Autowired private MyNewClass sut;
知道出了什么问题吗?
添加@ExtendWith(SpringExtension.class)
不足以创建 Spring 上下文。 您需要将@SpringBootTest
添加到您的MyNewClassTest
中。 根据相应的评论,您可以删除@ExtendWith(SpringExtension.class)
@SpringBootTest
class MyNewClassTest {
@Autowired private MyNewClass sut;
}