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

Spring integration 如何为Spring Integration DSL Http.outboundGateway操作编写JUnit,spring-integration,spring-integration-dsl,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

如何为以下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“”。最好是对URL中的服务器进行模拟,以便它响应一些响应

通过POST操作,我想检查是否向URL发送了一些JSON


这可以测试吗?

对于在客户端测试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 {

有关更多信息,请参阅文档:

谢谢,我尝试了这个。
@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();
    }

}