【问题标题】:How to write the JUnit for the Spring Integration DSL Http.outboundGateway operation如何为 Spring Integration DSL Http.outboundGateway 操作编写 JUnit
【发布时间】:2018-10-30 13:33:01
【问题描述】:

如何为以下 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

这可以测试吗?

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    为了在客户端测试 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

    【讨论】:

    • 谢谢我试试这个。
    猜你喜欢
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多