@SpringIntegrationTest 注释未按预期加载上下文 [英] @SpringIntegrationTest annotation does not load context as expected

查看:21
本文介绍了@SpringIntegrationTest 注释未按预期加载上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当我使用 @SpringBootTest 时,我会获得 bean 的完整上下文.我可以@Autowire 应用程序启动后可用的各种bean.现在,在 spring-integration-test 库的范围内,@SpringIntegrationTest 不会这样做.

Normally, when I use @SpringBootTest I get the full context of beans. I can the @Autowire all kinds of beans that are available after the application has started. Now, in the scope of spring-integration-test libary, the @SpringIntegrationTest does not do this.

正如测试模块所承诺的那样,您可以使用

As the testing module promises, you can use

@Autowired
private MockIntegrationContext mockIntegrationContext;

但是,在检查该实例上的 bean 映射后,我发现没有 bean!

However, after inspecting the bean map on that instance, I found out there are no beans!

示例测试:

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringIntegrationTest
public class AppTest {

    @Autowired
    private MockIntegrationContext mockIntegrationContext;

    @Test
    public void contextLoads() {
        // put breakpoint to inspect field
        System.out.println(mockIntegrationContext);
    }
}

然而,当我运行以下代码时,我得到了一个完整的上下文:

When I however run the following code, I get a complete context:

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@SpringBootTest
public class App2Test {

    @Autowired
    private ListableBeanFactory beanFactory;

    @Test
    public void contextLoads() {
        Assert.isTrue(beanFactory.getBeanDefinitionCount() > 0)
    }
}

这是为什么?如何使用 spring-integration-test 获得类似的结果?

Why is that? How can I achieve a similar result with spring-integration-test?

阅读材料:https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html

推荐答案

它们是独立的注解;两者都需要.

They are independent annotations; you need both.

编辑

这对我来说很好用:

@RunWith(SpringRunner.class)
@SpringBootTest
@SpringIntegrationTest
public class So52297757ApplicationTests {

    @Autowired
    private MockIntegrationContext mockIntegrationContext;

    @Autowired
    private String foo;

    @Test
    public void contextLoads() {
        System.out.println(foo);
        System.out.println(mockIntegrationContext);
    }

}

@SpringBootApplication
public class So52297757Application {

    public static void main(String[] args) {
        SpringApplication.run(So52297757Application.class, args);
    }

    @Bean
    public String foo() {
        return "foo";
    }

}

foo
org.springframework.integration.test.context.MockIntegrationContext@1de5f0ef

这篇关于@SpringIntegrationTest 注释未按预期加载上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆