简体   繁体   English

春季测试类是否解决了它的自动关联

[英]Spring Test if class resolved it's Autowired dependencies

I have a this class 我有一堂课

@Component("A")
public class A {
    private B b;

    @Autowired
    public A(B b) {
        this.b = b;
    }
}

and another class 和另一类

@Component("B")
public class B {

    public B() {

    }
}

Now I've written this test 现在我写了这个测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class ConstructorAutowiredTest {

    @Configuration
    static class ContextConfiguration {
        @Bean
        public B getBBean() {
            B b = new B();
            return b;
        }
    }

    @Autowired
    private B b;


    @Test
    public void isDependencyResolved() {
        assertNotNull(b);
    }
}

It works as well. 它也可以。 But is there any way to test whether this B bean was automatically injected in A . 但是有什么方法可以测试此B bean是否自动注入A I'm trying to so something like this but obviously it's not working. 我正在尝试这样的事情,但显然它没有用。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class ConstructorAutowiredTest {

    @Configuration
    static class ContextConfiguration {
        @Bean
        public B getBBean() {
            B b = new B();
            return b;
        }
    }

    @Autowired
    private A a;

    @Autowired
    private B b;


    @Test
    public void isDependencyResolved() {
        assertNotNull(b);
        assertNotNull(a);
    }
}

Exception: 例外:

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

You could have added to your configuration a component scan which uploads your @Component("A") 您可能已在组件配置中添加了一个组件扫描,该组件扫描会上传您的@Component(“ A”)

like this: 像这样:

@Configuration
@ComponentScan("some.package")
static class ContextConfiguration {
    @Bean
    public B getBBean() {
        B b = new B();
        return b;
    }
}

This would resolve your exception, and then you could assert your Bean A if it has B inserted. 这将解决您的异常,然后可以在Bean A插入B的情况下断言。

As far as I understand your question, I think you are using JUnit to test something that is not a unit test but something closer to an integration test, which is out of the scope of JUnit. 就我所理解的问题而言,我认为您正在使用JUnit测试不是单元测试而是更接近集成测试的东西,这超出了JUnit的范围。

JUnit will always assume that you have initialized properly the class to be tested, dependency resolutions aren't its responsability. JUnit将始终假定您已正确初始化了要测试的类,而依赖项解析不是它的责任。

And, as you have seen, Spring won't event let that test start if the dependencies are not solved 而且,如您所见,如果不解决依赖项,Spring将不会让该测试开始

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM