【问题标题】:Testing camel routes测试骆驼路线
【发布时间】:2020-01-20 16:48:11
【问题描述】:

我的项目中定义了多个路由类,位于 com.comp.myapp.routes 下。 为了测试这些,我正在模拟最终路线并检查/比较收到的交付。

比如说我有以下路线:

public class MyRoute1 extends RouteBuilder {
    public void configure() throws Exception {
       //Route_1 code
    }
}

public class MyRoute2 extends RouteBuilder {
    public void configure() throws Exception {
       //Route_2 code
    }
}

....
...//some route impl
..
public class MyRouteN extends RouteBuilder {
    public void configure() throws Exception {
       //Route_N code
    }
}

现在,对于所有这些路线,我编写的测试用例似乎都是一样的。 先模拟一下。

模拟 MyRoute1

public class MyRoute1_Mock extends RouteBuilder {
    public void configure() throws Exception {
      from("direct:sampleInput")
                .log("Received Message is ${body} and Headers are ${headers}")
                .to("mock:output");
    }
}

测试 MyRoute1:

public class MyRoute1_Test extends CamelTestSupport {
    @Override
    public RoutesBuilder createRouteBuilder() throws Exception {
        return new MyRoute1_Mock();
    }
    @Test
    public void sampleMockTest() throws InterruptedException {
        String expected="Hello";
        /**
         * Producer Template.
         */
        MockEndpoint mock = getMockEndpoint("mock:output");
        mock.expectedBodiesReceived(expected);
        String input="Hello";
        template.sendBody("direct:sampleInput",input );
        assertMockEndpointsSatisfied();
    }
}

现在要对其他类进行单元测试,只需复制并粘贴上述不同名称的代码,例如 MyRoute2_Test 、 MyRoute3_Test 、 ...MyRouteN_Test 。

那么它实际测试了什么? 它只是为了编写测试用例而编写的。 它实际上只是检查/测试模拟库和骆驼测试库是否工作我们的代码是否工作? 实际应该怎么做?

【问题讨论】:

    标签: java unit-testing junit apache-camel


    【解决方案1】:

    您想测试您的 Camel 路线,但在测试中您模拟了它们。所以是的,您正在测试您的路线模拟而不是真实路线。

    测试你的真实路线:

    • 发送消息到您的真实路线 from端点
    • 如果这不容易,模拟您的from 端点(不是整个路由!),将其替换为direct 端点。使用adviceWith 这很容易
    • 测试消息正在通过你的路由
    • 通过模拟这些端点来断言任何to 端点都会收到正确的消息。同样,为此使用adviceWith。当然还有Camel Mock
    • 您可以从 Camel Mock 获取收到的消息(交换)以进行深入断言
    • 如果您获得了满意的测试,请开始通过在您的路线中注入错误来编写否定测试adviceWith 也可以在这里提供帮助
    • ...等等

    如果您完全不熟悉 Camel 路线测试,请获取 Camel in Action 2nd edition。它在 65 页上解释了所有提到的 Camel 应用程序的测试方面。当然,它还会带您在更多页面上完整地穿越 Camel 世界。

    顺便说一句:如果测试你的路线很困难,它们太复杂了。开始划分您的路线,以便它们易于测试。

    【讨论】:

      【解决方案2】:

      您显示的路线实际上对穿过它的消息没有任何作用,因此测试您在一端发送的相同文本是否出现在另一端就是所有需要测试的。

      对于具有更多数据转换和处理的路由,您可以测试输出数据类型,在需要时调用处理器,您可以模拟抛出异常等。上面的内容是一个好的开始。

      【讨论】:

        【解决方案3】:

        在线解释,希望这可以帮助您理解 Mock 在单元测试中的重要性:

         public void sampleMockTest() throws InterruptedException {
            String expected="Hello"; 
            MockEndpoint mock = getMockEndpoint("mock:output");//Mocking endpoint
            mock.expectedBodiesReceived(expected); //Setting expected output to mocked endpoint
            String input="Hello";
            template.sendBody("direct:sampleInput",input );//triggering route execution by sending input to route
            assertMockEndpointsSatisfied(); //Verifies if input is equal to output
        }
        

        如果您的端点是 Rest 服务,您可以使用“TestRestTemplate”而不是像下面这样模拟和测试:

        import org.junit.runner.RunWith;
        import org.springframework.boot.test.context.SpringBootTest;
        import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
        import org.springframework.test.context.junit4.SpringRunner;
        
        @RunWith(SpringRunner.class)
        @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
        public class SampleCamelApplicationTest {
        }
        import org.springframework.boot.test.web.client.TestRestTemplate;
        
        @RunWith(SpringRunner.class)
        @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
        public class SampleCamelApplicationTest {
           @Autowired
           private TestRestTemplate restTemplate;
        }
        @Test
        public void sayHelloTest() {
           // Call the REST API
           ResponseEntity<String> response = restTemplate.getForEntity("/camel/hello", String.class);
           assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
           String s = response.getBody();
           assertThat(s.equals("Hello World"));
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-19
          • 2023-03-03
          • 1970-01-01
          • 2018-09-05
          • 2011-05-24
          相关资源
          最近更新 更多