@SpringIntegrationTest annotation does not load context as expected

為{幸葍}努か 提交于 2019-12-25 00:33:11

问题


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;

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

Example test:

@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)
    }
}

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

Reading materials: https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html


回答1:


They are independent annotations; you need both.

EDIT

This works fine for me:

@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);
    }

}

and

@SpringBootApplication
public class So52297757Application {

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

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

}

and

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


来源:https://stackoverflow.com/questions/52297757/springintegrationtest-annotation-does-not-load-context-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!