Zuul 过滤器的 SpringBoot Junit 测试

SpringBoot Junit testing for filters in Zuul

我是 Zuul J 单元测试的新手。我有几个过滤器,它们是 ChangeRequestEntityFilter 和 SessionFilter,我在下面粘贴了我的过滤器代码。谁能告诉我如何为过滤器编写 Junit。我已经搜索并尝试使用 MockWire 进行单元测试(我还粘贴了带有基本注释和 WireMock 端口的空方法)。我至少需要一个适当的例子,说明 Zuul 的 J-unit 是如何工作的。我已经参考了 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

"When @MockBean is used on a field, as well as being registered in the application context, the mock will also be injected into the field. Typical usage might be:"

@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 的问题是如果您在过滤器中有服务,那么它将是一个集成测试这将更难实施。