【问题标题】:UnsatisfiedDependencyException in Springboot TestSpring Boot 测试中的 UnsatisfiedDependencyException
【发布时间】:2019-07-31 15:00:57
【问题描述】:

我必须做些什么来解决,尽管 Springboot 应用程序按照测试的方式自动配置,但测试类无法解决自动装配的依赖关系?当然,我没有实例化自己的对象。有简单的方法吗?

班级:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PostClient.class, NumberplateClientCommands.class})
public class NumberplateClientCommandsTest {

    private StandardMethodTargetRegistrar registrar = new StandardMethodTargetRegistrar();
    private ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry();
    private Map<String, MethodTarget> commands;

    @Autowired
    public PostClient postClient;

    @Before
    public void setUp() {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(NumberplateClientCommands.class);
        registrar.setApplicationContext(context);
        registrar.register(registry);
    }

    @Test
    public void shouldAddTwoIntegers() {
        commands = registry.listCommands();

        MethodTarget methodTarget = commands.get("sum");

        assertThat(methodTarget, notNullValue());
        Assertions.assertThat(methodTarget.getGroup()).isEqualTo(
                "Numberplate Client Commands");
        assertThat(methodTarget.getHelp(), Is.is("Add up to sum."));
        assertThat(methodTarget.getMethod(), is(
                ReflectionUtils.findMethod(NumberplateClientCommands.class, "sum", int.class,
                        int.class)));
        assertThat(methodTarget.getAvailability().isAvailable(), is(true));
        assertEquals(3, ReflectionUtils.invokeMethod(methodTarget.getMethod(),
                methodTarget.getBean(), 1, 2));
    }

    @Test
    public void shouldSayHi() {
        commands = registry.listCommands();
        MethodTarget methodTarget = commands.get("say-hi");

        assertThat(methodTarget, notNullValue());
        Assertions.assertThat(methodTarget.getGroup()).isEqualTo(
                "Numberplate Client Commands");
        assertThat(methodTarget.getHelp(), Is.is("Saying hi to a given person's name."));
        assertThat(methodTarget.getMethod(), is(
                ReflectionUtils.findMethod(NumberplateClientCommands.class, "sayHi", String.class)));
        assertThat(methodTarget.getAvailability().isAvailable(), is(true));
        assertEquals("Hi, Nadine", ReflectionUtils.invokeMethod(methodTarget.getMethod(),
                methodTarget.getBean(), "Nadine"));
    }

    @Test
    public void shouldPostCamImage2kafka() throws FileNotFoundException {
        commands = registry.listCommands();
        MethodTarget methodTarget = commands.get("one");
        NumberPlateUtility np = new NumberPlateUtility();
        HttpStatus response = postClient.postNumberPlate(np.completeImage());
        assertThat(response, is(HttpStatus.OK));
    }
}

例外是:

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

我分享了@98​​7654321@,如果有帮助的话。其他问题和答案显示了非常具体的解决方案,这些解决方案通常取决于一些 @Repository 或其他类似 MVC 的组件、接口或复杂的控制器,而我没有。

【问题讨论】:

    标签: autowired spring-boot-test


    【解决方案1】:

    发生这种情况是因为您在测试的 setUp(before) 方法中创建了另一个应用程序上下文,并在那里只选择了一个配置文件 (NumberplateClientCommands)。

    我可以建议你尝试这样的事情:

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = {PostClient.class, NumberplateClientCommands.class})
    public class NumberplateClientCommandsTest {
    
        ...
    
        @Autowired
        public ApplicationContext applicationContext;
    
        @Before
        public void setUp() {
            registrar.setApplicationContext(applicationContext);
            registrar.register(registry);
        }
    
        @Test
        public void shouldAddTwoIntegers() {
        ...
        }
    
    

    【讨论】:

      猜你喜欢
      • 2020-10-19
      • 2019-05-01
      • 2017-03-31
      • 1970-01-01
      • 2020-04-14
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      相关资源
      最近更新 更多