使用spring集成进行集成测试

使用spring集成进行集成测试,spring,spring-integration,Spring,Spring Integration,我有一个问题: 我有两个组件A和B(两场战争): A用于存储(Neo4J),B用于搜索(Elasticsearch) 组件之间的通信由Spring集成使用HTTP入站/出站进行管理 以下是我的spring集成配置文件: 服务器A(存储) 另一方面: 服务器B: <int:annotation-config/> <beans:bean class="org.springframework.integration.http.inbound.UriPathHandlerM

我有一个问题:

我有两个组件A和B(两场战争):

A用于存储(Neo4J),B用于搜索(Elasticsearch)

组件之间的通信由Spring集成使用HTTP入站/出站进行管理

以下是我的spring集成配置文件: 服务器A(存储)


另一方面: 服务器B:

    <int:annotation-config/>
<beans:bean class="org.springframework.integration.http.inbound.UriPathHandlerMapping"/>

<int-http:inbound-gateway request-channel="searchRequest"
                          reply-channel="searchReply"
                          path="/api/es"
                          request-payload-type="xxxx.AuditChange"
                          supported-methods="POST"/>


<int:gateway id="searchGateway"
             service-interface="xxxx.IAbSearchResult"/>

<int-http:outbound-gateway auto-startup="true"
                           request-channel="searchResult"
                           url="http://localhost:9080/A/api/searchResult"/>


<int:json-to-object-transformer id="jsonToObject" input-channel="searchRequest"
                                type="xxxxxx.AuditChange"/>
<int:object-to-json-transformer id="objectToJson" input-channel="searchReply" output-channel="searchResult"/>

<int:channel id="searchRequest"/>
<!--<int:channel id="esDelete"/>-->
<int:channel id="searchReply"/>
<int:channel id="searchResult"/>

我的问题:

我想做一个从服务器A到服务器B再到服务器A的集成测试

最好的策略是什么? 有可能不用嘲笑就可以做到吗? 不启动服务器就可以做到这一点吗?(服务器A和B关闭)

致意
Nabil Belakbir

我不确定您是否希望在集成测试中使用它

您可以使用SpringMVC测试框架来测试入站网关,而无需启动任何服务器

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { /*omitted*/ })
@WebAppConfiguration
public class HttpInboundGatewayIntegrationTests {
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setup() throws Exception {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testInboundGateway()
        throws Exception {
    //

        mockMvc.perform(get("/api/searchResult").
            param("aParam", aParam).
            param("anotherParam",anotherParam)).
            andExpect(status().isForbidden());
}
但是如果您只想测试网关,那么最好将spring配置转换成不同的xml(例如,对于InboundGateway单独使用a-http-gateway.xml)


另一方面,必须在没有存根或模拟的情况下为测试出站网关启动服务器。可能您对它感兴趣,它是一个简单的http服务器存根框架。

有关如何独立测试流的想法,请参阅和。

感谢您的回复。我的问题是服务器A和服务器B完全分开。因此,SpringMVC测试将允许我在不启动服务器A的情况下测试InboundGateway。我也可以通过直接测试网关来实现这一点。所以问题是服务器A何时会试图到达服务器B。在这种情况下,必须启动或模拟服务器B。我没有找到正确的方法。还感谢moco,这对我的情况很有帮助
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { /*omitted*/ })
@WebAppConfiguration
public class HttpInboundGatewayIntegrationTests {
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;

    @Before
    public void setup() throws Exception {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testInboundGateway()
        throws Exception {
    //

        mockMvc.perform(get("/api/searchResult").
            param("aParam", aParam).
            param("anotherParam",anotherParam)).
            andExpect(status().isForbidden());
}