SpringBoot Junit在Zuul测试过滤器



我是Zuul J单元测试的新手。我有两个过滤器,它们是ChangeRequestEntityFilter和SessionFilter,我在下面粘贴了我的过滤器代码。有人能告诉我如何为过滤器写一个Junit吗。我已经搜索并尝试使用MockWire进行单元测试(此外,我还粘贴了带有基本注释和WireMock端口的空方法(。我至少需要一个合适的例子,说明祖尔的J部队是如何运作的。我已经推荐了http://wiremock.org/docs/getting-started/文件。我在哪里得到了该做什么,但没有得到如何做。

public class ChangeRequestEntityFilter extends ZuulFilter {
@Autowired
private UtilityHelperBean utilityHelperBean;
@Override
public boolean shouldFilter() {
// //avoid http GET request since it does'nt have any request body
return utilityHelperBean.isValidContentBody();
}
@Override
public int filterOrder() {
//given priority
}
@Override
public String filterType() {
// Pre
}
@Override
public Object run() {
RequestContext context = getCurrentContext();
try {
/** get values profile details from session */
Map<String, Object> profileMap = utilityHelperBean.getValuesFromSession(context,
CommonConstant.PROFILE.value());
if (profileMap != null) {
/** get new attributes need to add to the actual origin microservice request payload */
Map<String, Object> profileAttributeMap = utilityHelperBean.getProfileForRequest(context, profileMap);
/** add the new attributes in to the current request payload */
context.setRequest(new CustomHttpServletRequestWrapper(context.getRequest(), profileAttributeMap));
}
} catch (Exception ex) {
ReflectionUtils.rethrowRuntimeException(new IllegalStateException("ChangeRequestEntityFilter : ", ex));
}
return null;
}
}

我知道,我要求更多。但给我任何简单的工作完整的例子,我很满意。

我当前的代码带有基本注释和WireMock端口。

@RunWith(SpringRunner.class)
@SpringBootTest
@DirtiesContext
@EnableZuulProxy
public class ChangeRequestEntityFilterTest {
@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);
@Mock
ChangeRequestEntityFilter requestEntityFilter;
int port = wireMockRule.port();
@Test
public void changeRequestTest() {
}
}

你试过@MockBean吗?

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/mock/mockito/MockBean.html

"当@MockBean在字段上使用时,以及在应用程序上下文中注册时,mock也会被注入到字段中。典型的用法可能是:">

@RunWith(SpringRunner.class)
public class ExampleTests {
@MockBean
private ExampleService service;
@Autowired
private UserOfService userOfService;
@Test
public void testUserOfService() {
given(this.service.greet()).willReturn("Hello");
String actual = this.userOfService.makeUse();
assertEquals("Was: Hello", actual);
}
@Configuration
@Import(UserOfService.class) // A @Component injected with ExampleService
static class Config {
}
}

这里有另一种方法:

private ZuulPostFilter zuulPostFilter;
@Mock
private anotherService anotherService;
@Mock
private HttpServletRequest request;
@Before
public void before() {
MockitoAnnotations.initMocks(this);
MonitoringHelper.initMocks();
zuulPostFilter = new ZuulPostFilter(anotherService);
doNothing().when(anotherService).saveInformation(null, false);
}
@Test
public void postFilterTest() {
log.info("postFilterTest");
RequestContext context = new RequestContext();
context.setResponseDataStream(new ByteArrayInputStream("Test Stream".getBytes()));
context.setResponseGZipped(false);
RequestContext.testSetCurrentContext(context);
when(request.getScheme()).thenReturn("HTTP");
RequestContext.getCurrentContext().setRequest(request);
ZuulFilterResult result = zuulPostFilter.runFilter();
assertEquals(ExecutionStatus.SUCCESS, result.getStatus());
assertEquals("post", zuulPostFilter.filterType());
assertEquals(10, zuulPostFilter.filterOrder());
}

在这种情况下,您可以测试过滤器并模拟其中的服务,而不必自动连接它,@autowired的问题是,如果过滤器中有服务,那么它将是一个更难实现的集成测试。

最新更新