简体   繁体   English

为什么@Autowired 会引发 UnsatisfiedDependencyException,甚至 class 也没有实现任何接口?

[英]Why @Autowired raises UnsatisfiedDependencyException, even the class does not implement any interface?

Here is the class I want to test这是我要测试的 class

@Component
public class PermissionCheck {

    @Autowired
    private MyEntityRepository myEntityRepository;

    public boolean hasPermission(int myEntityID) {
        MyEntity myEntity = myEntityRepository.findById(myEntityId);
        return myEntity != null;
    }
}

Here is the test class这里是测试 class

@RunWith(SpringRunner.class)
public class PermissionCheckTests {

    @Autowired                                     // you need to autowire
    private PermissionCheck permissionCheck;       // and it uses @MockBean dependency

    @MockBean                                      // if no such @MockBean exists
    private MyEntityRepository myEntityRepository; // the real implementation is used

    @Test
    public void shouldHasPermission() {
        MyEntity myEntity = new MyEntity();

        when(this.myEntityRepository.findById(any())).thenReturn(myEntity);
        assertTrue(this.permissionCheck.hasPermission(0));
    }
}

And when I run this test I got当我运行这个测试时,我得到了

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'PermissionCheckTests': 
Unsatisfied dependency expressed through field 'permissionCheck'; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'PermissionCheck' available: 
expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true), 
@org.springframework.beans.factory.annotation.Qualifier(value="")}

From other SO questions, such as this one , I see that this happens when the target class implements an interface.从其他 SO 问题(例如这个问题)中,我看到当目标 class 实现接口时会发生这种情况。 But as we can see from the code, PermissionCheck does not implement any interface, so why is the exception still thrown?但是从代码中我们可以看出, PermissionCheck没有实现任何接口,那为什么还是抛出异常呢?

Does that mean I have to create an interface, @Autowired it and let PermissionCheck implement it?这是否意味着我必须创建一个接口,@Autowired 并让PermissionCheck实现它? It seems redundant to me, since I don't see why I need such an interface.这对我来说似乎是多余的,因为我不明白为什么我需要这样的接口。 Is there a way to make it work without creating a new interface which I will solely use for this purpose?有没有办法让它工作而无需创建我将仅用于此目的的新界面?

@RunWith(SpringRunner.class) does not automatically load your configurations/beans/properties unless accompanied with annotations that specifically do that, such as @ContextConfiguration and @TestPropertySource . @RunWith(SpringRunner.class)不会自动加载您的配置/beans/properties,除非附带专门执行此操作的注释,例如@ContextConfiguration@TestPropertySource

If yours is a Spring Boot application and you want to load the whole context along with all properties, you can simply add the @SpringBootTest annotation to your test class.如果您的应用程序是 Spring 引导应用程序并且您想要加载整个上下文以及所有属性,您可以简单地将@SpringBootTest注释添加到您的测试 class 中。

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这个类编译即使它没有正确实现其接口? - Why does this class compile even though it does not properly implement its interface? 为什么ZonedDateTime类没有实现TemporalAdjuster接口 - why ZonedDateTime class does not implement TemporalAdjuster interface 当自动装配的依赖项替换为接口或抽象类时,为什么这个 Spring Boot MVC 测试会失败? - Why does this Spring Boot MVC test fail when autowired dependency replaced with interface or abstract class? 为什么内部类必须强制实现其接口方法,即使外部类具有接口方法也是如此? - Why is an inner class forced to implement its interface methods, even if the outer class has it? JAXB为什么说“ xxx是一个接口,而JAXB无法处理接口”。 即使生成的类不是接口 - Why does JAXB say “xxx is an interface, and JAXB can't handle interfaces”. Even though the generated class is not an interface Java 中的抽象类不需要从其实现接口实现任何方法。 为什么? - An abstract class in Java need not implement any methods from its implementing interface. Why? 为什么按类@Autowired 电线? - Why @Autowired wires by class? 不满意的DependencyException在构造函数上自动装配并且设置不起作用 - UnsatisfiedDependencyException autowired on constructor and set not working Spring集成测试中的@Autowired和UnsatisfiedDependencyException - @Autowired and UnsatisfiedDependencyException in Spring integration test 类 SpringHibernateJpaPersistenceProvider 没有实现请求的接口 PersistenceProvider - Class SpringHibernateJpaPersistenceProvider does not implement the requested interface PersistenceProvider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM