简体   繁体   English

弹簧集成的集成测试

[英]Integration testing with spring integration

I have a problem : 我有个问题 :

I have two component A and B (Two wars ) : 我有两个组件A和B(两场战争):

A for storage(Neo4J) and B for search(Elasticsearch). A用于存储(Neo4J)和B用于搜索(Elasticsearch)。

the communication between the components is managed by Spring Integration using HTTP Inbound/Outbound . 组件之间的通信由Spring Integration使用HTTP Inbound / Outbound进行管理。

Here is MY configuration files for spring integration : Server A (Storage) 这是Spring集成的MY配置文件:服务器A(存储)

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

 <int:gateway id="searchGateway"
             service-interface="xxxxx.IAuditSearchService"/>

<int-http:outbound-gateway auto-startup="true"
                           request-channel="sendRequest"
                           url="http://localhost:8081/B/api/es"
                           extract-request-payload="true"/>

<int-http:inbound-gateway request-channel="searchResult"
                          request-payload-type="xxxxx.SearchResult"
                          path="/api/searchResult"
                          supported-methods="POST"/>

<int:object-to-json-transformer auto-startup="true"
                                id="objectToJson" input-channel="searchRequest"
                                output-channel="sendRequest">
</int:object-to-json-transformer>

<int:json-to-object-transformer input-channel="searchReply"
                                auto-startup="true"
                                id="jsonToObject" output-channel="searchResult"
                                type="xxxxxxxx.SearchResult">
</int:json-to-object-transformer>

<int:channel id="searchRequest"/>
<int:channel id="sendRequest"/>
<int:channel id="searchReply"/>
<int:channel id="searchResult"/>

In the other side : Server B : 在另一边:服务器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"/>

My question : 我的问题 :

I want to do an integration test from the server A to Server B To server A . 我想从服务器A到服务器B到服务器A进行集成测试。

What's the best strategy ? 什么是最好的策略? It's possible to do it without mocking ? 没有嘲笑可以做到这一点吗? It's possible to do it without starting the servers ? 没有启动服务器就可以做到这一点? (Servers A and B down) (服务器A和B向下)

Best Regards Nabil Belakbir 最好的问候Nabil Belakbir

I'm not sure is this you want for integration tests. 我不确定你是否想进行集成测试。

You can use Spring mvc test framework to test inbound gateway without starting up any server. 您可以使用Spring mvc测试框架来测试入站网关,而无需启动任何服务器。

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

But if you want to test the gateway only, you'd better sperate the spring configuration into different xmls(a-http-gateway.xml for the InboundGateway alone, for example). 但是如果你只想测试网关,你最好将spring配置编入不同的xmls(例如,仅针对InboundGateway的a-http-gateway.xml)。

On the other hand, A server has to be started up for test outbound gateway without stubbing or mocking. 另一方面,必须为测试出站网关启动服务器而不进行存根或模拟。 Maybe you are interested in https://github.com/dreamhead/moco , it's a simple framework to stub http server. 也许你对https://github.com/dreamhead/moco感兴趣,它是一个简单的框架来存根http服务器。

有关如何独立测试流的想法,请参阅测试示例高级测试示例

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

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