【问题标题】:Writing Test cases for Spring Integration Application为 Spring 集成应用程序编写测试用例
【发布时间】:2021-03-28 16:22:33
【问题描述】:

我是单元/集成测试的新手。因此寻找给定场景的最佳方法。 我有用 Spring Integration 编写的遗留应用程序 (REST)。我已迁移到 Spring Boot 的同一应用程序。现在我想知道,如何在控制器中模拟网关以及如何测试网关、路由器。应用结构为:

控制器:

@RestController
@RequestMapping(value = "/acct")
public class AcctOpenImpl implements AcctOpen {
    /**
     * AcctMgmt Gateway Interface.
     */
    @Autowired
    AcctOpenService acctopenmgmtServiceGateway;
    
    @Override
    @RequestMapping(value = "/product/{product_id_num}", method = RequestMethod.GET)
    public ResponseEntity<RetrieveAppProdAplnResponse> rtrvAppProdAplnByQueryParam(
        @PathVariable("product_id_num") final Long productIdNum) throws BusinessException,
        TechnicalException {
        Map<String, Object> headers = getHeaders();
       
        RetrieveAppProdAplnResponse response = this.acctopenmgmtServiceGateway.rtrvAppProdApln(request, headers);

        ResponseEntity<RetrieveAppProdAplnResponse> retrieveAppProdResponse = new ResponseEntity<RetrieveAppProdAplnResponse>(
            response, HttpStatus.OK);

        return retrieveAppProdResponse;
    }
} 

服务接口:

public interface AcctOpenService {
    public RetrieveAppProdAplnResponse rtrvAppProdApln(RetrieveAppProdAplnRequest retrieveAppProdAplnRequest,
        @Headers Map<String, Object> customHeaderMap) throws BusinessException, TechnicalException;
}

geteways.xml:

<int:gateway id="acctopenmgmtServiceGateway" service-interface="com.hsbc.group.depositproduct.svc.gateway.AcctOpenService"
    default-reply-channel="acctopenmgmtReplyChannel" error-channel="acctopenmgmtErrorChannel">
    <int:method name="rtrvAppProdApln" request-channel="rtrvAppProdAplnRequestChannel" />
</int:gateway>

si-channels.xml:

<int:channel id="rtrvAppProdAplnRequestChannel"/>

si-routers.xml:

<int:router input-channel="rtrvAppProdAplnRequestChannel" default-output-channel="acctopenmgmtReplyChannel"     expression="headers.SERVICE_HEADER.consumerEntyId.ctryCde.toLowerCase()+headers.SERVICE_HEADER.consumerEntyId.groupMbr+'RtrvAppProdAplnRequestChannel'" />

si-chain.xml:

<int:chain input-channel="usMMBIRtrvAppProdAplnRequestChannel" output-channel="acctopenmgmtReplyChannel">
    <int:transformer method="formatRequest" ref="usRtrvAppProdAplnTransformer"/>
    <int:service-activator method="rtrvAppProdApln" ref="rtrvAppProdAplnActivator" />
    <int:transformer method="parseResponse" ref="usRtrvAppProdAplnTransformer"/>
</int:chain>

si-transformers.xml:

<bean id="usRtrvAppProdAplnTransformer" class="com.hsbc.group.depositproduct.us.svc.transformer.RetrieveAppProdAplnTransformer">
</bean>

您能否建议给定场景的最佳方法(单元测试或集成测试)。另外,如果有人举出任何例子说明我该如何开始,那将不胜感激。 提前致谢。

【问题讨论】:

    标签: spring-boot unit-testing spring-integration integration-testing


    【解决方案1】:

    不完全清楚你想测试什么,但感觉更像是你想真正测试那个 REST 调用。考虑研究一下什么是 Spring Mock MVC,以及 Spring Boot 的一些特性:

    https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#spring-mvc-test-client

    https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-rest-templates-test-utility

    【讨论】:

    • 感谢您的回复。实际上我想测试我的 REST API,但我想知道如何测试网关、路由器。
    • 网关只不过是一个简单的服务,用于注入测试用例和调用。路由器将rtrvAppProdAplnRequestChannel 作为发送消息的输入。在 Spring Integration 文档中查看更多信息:docs.spring.io/spring-integration/docs/current/reference/html/…
    最近更新 更多