如何为Spring Integration DSL Http.outboundGateway操作编写JUnit

时间:2018-10-30 13:33:02

标签: spring-integration spring-integration-dsl

如何为以下Spring Integration DSL Http.outboundGateway操作编写JUnit:

    integrationFlowBuilder
       .from(integerMessageSource(),
            c -> c.poller(Pollers.cron("0 0/1 * 1/1 * ?")
       .handle(Http
            .outboundGateway("http://localhost:8050/greeting")
            .httpMethod(HttpMethod.GET)
            .expectedResponseType(String.class))
       .channel("getChannel");

    flowContext.registration(integrationFlowBuilder.get()).register();

integerMessageSource方法是

    @Bean
    private MethodInvokingMessageSource integerMessageSource() {
        final MethodInvokingMessageSource source = new MethodInvokingMessageSource();
        source.setObject(new AtomicInteger());
        source.setMethodName("getAndIncrement");
        return source;
    }

我想用一些cron表达式启动JUnit,并验证是否调用了URL“ http://localhost:8050/greeting”。最好模拟URL http://localhost:8050/greeting中的服务器,以便它响应一些响应。

通过POST操作,我想检查是否有一些JSON发送到URL http://localhost:8050/greeting

可以测试吗?

1 个答案:

答案 0 :(得分:1)

为了在客户端测试REST,Spring Test Framework提供了以下功能:

/**
 * <strong>Main entry point for client-side REST testing</strong>. Used for tests
 * that involve direct or indirect use of the {@link RestTemplate}. Provides a
 * way to set up expected requests that will be performed through the
 * {@code RestTemplate} as well as mock responses to send back thus removing the
 * need for an actual server.
 *
 * <p>Below is an example that assumes static imports from
 * {@code MockRestRequestMatchers}, {@code MockRestResponseCreators},
 * and {@code ExpectedCount}:
 *
 * <pre class="code">
 * RestTemplate restTemplate = new RestTemplate()
 * MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();
 *
 * server.expect(manyTimes(), requestTo("/hotels/42")).andExpect(method(HttpMethod.GET))
 *     .andRespond(withSuccess("{ \"id\" : \"42\", \"name\" : \"Holiday Inn\"}", MediaType.APPLICATION_JSON));
 *
 * Hotel hotel = restTemplate.getForObject("/hotels/{id}", Hotel.class, 42);
 * &#47;&#47; Use the hotel instance...
 *
 * // Verify all expectations met
 * server.verify();
 * </pre>
 *
 * <p>Note that as an alternative to the above you can also set the
 * {@link MockMvcClientHttpRequestFactory} on a {@code RestTemplate} which
 * allows executing requests against an instance of
 * {@link org.springframework.test.web.servlet.MockMvc MockMvc}.
 *
 * @author Craig Walls
 * @author Rossen Stoyanchev
 * @since 3.2
 */
@SuppressWarnings("deprecation")
public final class MockRestServiceServer {

Http.outboundGateway()可以与RestTemplate一起配置,并且该MockRestServiceServer必须使用该@ContextConfiguration @RunWith(SpringJUnit4ClassRunner.class) @DirtiesContext public class OutboundResponseTypeTests { @Autowired private RestTemplate restTemplate; private MockRestServiceServer mockServer; @Before public void setup() { this.mockServer = MockRestServiceServer.createServer(this.restTemplate); } @Test public void testDefaultResponseType() throws Exception { this.mockServer.expect(requestTo("/testApps/outboundResponse")) .andExpect(method(HttpMethod.POST)) .andRespond(withSuccess(HttpMethod.POST.name(), MediaType.TEXT_PLAIN)); ... this.mockServer.verify(); } } 。像这样:

command: ["python2", "/usr/bin/supervisord", "--nodaemon", "--configuration", "/etc/supervisor/supervisord.conf"]

有关更多信息,请参阅文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-client