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

How to write the JUnit for the Spring Integration DSL Http.outboundGateway operation

如何为以下 Spring 集成 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" is called. It would be good that the server in the URL http://localhost:8050/greeting 将被模拟,以便它响应一些响应。

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

这可以测试吗?

为了在客户端测试 REST,Spring 测试框架提供了这个:

/**
 * <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();
    }

}

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