[英]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.