简体   繁体   English

Spring Cloud Gateway 自定义过滤器工厂的单元测试

[英]Unit testing for Spring Cloud Gateway Custom Filter Factory

I have implemented the custom filter factories for Cloud Gateway.我已经为 Cloud Gateway 实现了自定义过滤器工厂。 However, I couldn't figure out the way to write unit testcases.但是,我无法弄清楚编写单元测试用例的方法。 While exploring the default Filter Factories test cases, I found that majority of factories test classes extends BaseWebClientTests and other classes which are in test package.在探索默认的过滤器工厂测试用例时,我发现大多数工厂测试类扩展了BaseWebClientTests和测试包中的其他类。

My question is that shall I copy paste those intermediate test classes to my local test package?我的问题是我应该将这些中间测试类复制粘贴到我的本地测试包中吗? What's community recommendation here?这里的社区推荐是什么?

Here is my result, for your information这是我的结果,供您参考

class CustomGatewayFilterFactoryTest {

    @Autowired
    private CustomGatewayFilterFactory factory;

    private ServerWebExchange exchange;
    private GatewayFilterChain filterChain = mock(GatewayFilterChain.class);
    private ArgumentCaptor<ServerWebExchange> captor = ArgumentCaptor.forClass(ServerWebExchange.class);

    @BeforeEach
    void setup() {
        when(filterChain.filter(captor.capture())).thenReturn(Mono.empty());
    }

    @Test
    void customTest() {
        MockServerHttpRequest request = MockServerHttpRequest.get(DUMMY_URL).build();
        exchange = MockServerWebExchange.from(request);
        GatewayFilter filter = factory.apply(YOUR_FACTORY_CONFIG);
        filter.filter(exchange, filterChain);
        // filter.filter(exchange, filterChain).block(); if you have any reactive methods

        ServerHttpRequest actualRequest = captor.getValue().getRequest();

        // Now you can assert anything in the actualRequest
        assertEquals(request, actualRequest);
    }

}

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

相关问题 使用 spock 对 spring 云网关过滤器进行单元测试 - Unit testing spring cloud gateway filters with spock Spring Cloud Gateway Filter Factory for Fire and Forget Model - Spring Cloud Gateway Filter Factory for Fire and Forget Model 如何使用 Spring 云网关自定义过滤器过滤每个请求? - How to use a Spring Cloud Gateway Custom Filter to filter every request? 如何在 application.yml Spring Cloud Gateway 中指定自定义过滤器 - How to Specify custom filter in application.yml Spring Cloud Gateway 如何在 Spring 云网关中添加特定于路由的自定义过滤器 - How to add a custom filter specific to a route in Spring Cloud Gateway 单元测试中的Spring Cloud Gateway注入不起作用 - Spring Cloud Gateway injection in Unit Tests is not Working Spring cloud sleuth ExtraFieldPropagation在Spring cloud Gateway Filter中失败 - Spring cloud sleuth ExtraFieldPropagation fail in Spring cloud Gateway Filter Junit 在 Spring 中测试自定义过滤器 - Junit Testing a Custom Filter in Spring Spring 网关应用程序找不到自定义过滤器 - Spring gateway application unable to find custom filter 单元测试存储库spring org.springframework.beans.factory.UnsatisfiedDependencyException - Unit testing repository spring org.springframework.beans.factory.UnsatisfiedDependencyException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM