簡體   English   中英

使用@SpringBootTest進行Spring啟動和駝峰測試

[英]Spring boot and camel testing with @SpringBootTest

我有春季啟動應用程序,彈簧啟動版本1.5.8和camel 2.20.1

簡單路線:

@Component
public class MyRoute extends RouteBuilder {

  public static final String IN = "file://in";

  public static final String OUT = "file://out";

  @Override
  public void configure() throws Exception {
    from(IN).routeId("myId").to(OUT);
  }
}

簡單的測試:

//@SpringBootTest
public class MyRouteTest extends CamelTestSupport {


      @Produce(uri = MyRoute.IN)
      private ProducerTemplate producerTemplate;

      @EndpointInject(uri = "mock:file:out")
      private MockEndpoint mockEndpointOut;

      @Override
      public String isMockEndpoints() {
        return "*";
      }

      @Test
      public void simpleTest() throws Exception {
        mockEndpointOut.expectedMessageCount(1);
        producerTemplate.sendBody("Test");
        mockEndpointOut.assertIsSatisfied();
      }

      @Override
      protected RoutesBuilder createRouteBuilder() throws Exception {
        return new MyRoute();
      }

    }

當我運行此測試時它運行正常,我得到一條消息,並且端點滿意。 如果我添加@SpringBootTest注釋,測試失敗? 為什么? 當我運行maven clean install時也沒有注釋它也會失敗:(

任何人都知道這個注釋對駱駝測試有什么作用,或者我如何調整它以使其有效?

謝謝

以下是最后的工作:

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
public class MyRouteTest2 {

  @Autowired
  private ProducerTemplate producerTemplate;

  @EndpointInject(uri = "mock:file:out")
  private MockEndpoint mockCamel;

  @Test
  public void test() throws InterruptedException {
    String body = "Camel";
    mockCamel.expectedMessageCount(1);

    producerTemplate.sendBody("file:in", body);

    mockCamel.assertIsSatisfied();
  }
}

嘗試添加注釋@RunWith(CamelSpringBootRunner.class) 根據文件

CamelSpringTestSupport的功能CamelSpringTestSupport基於Spring Boot Test的測試用例的實現。 這種方法允許開發人員使用典型的測試開發Spring測試約定來實現基於Spring Boot的應用程序/路由的測試。

另外,請考慮添加@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)@DisableJmx(true) 第一個在每個測試方法之后清除Spring上下文,這將避免在同一測試用例中從其他測試中留下任何后果(比如在沒有理由的情況下使測試失敗)。

后者將禁用JMX,因為您的測試不需要它。

官方文檔提供了有關使用Spring Boot運行Apache Camel的更多信息。 這里有一個示例摘錄:

@ActiveProfiles("test")
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(true)
public class MyRouteTest extends CamelTestSupport {

    @Autowired
    private CamelContext camelContext;

    @Override
    protected CamelContext createCamelContext() throws Exception {
        return camelContext;
    }

    @EndpointInject(uri = "direct:myEndpoint")
    private ProducerTemplate endpoint;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        RouteDefinition definition = context().getRouteDefinitions().get(0);
        definition.adviceWith(context(), new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                onException(Exception.class).maximumRedeliveries(0);
            }
        });
    }

    @Override
    public String isMockEndpointsAndSkip() {
            return "myEndpoint:put*";
    }

    @Test
    public void shouldSucceed() throws Exception {
        assertNotNull(camelContext);
        assertNotNull(endpoint);

        String expectedValue = "expectedValue";
        MockEndpoint mock = getMockEndpoint("mock:myEndpoint:put");
        mock.expectedMessageCount(1);
        mock.allMessages().body().isEqualTo(expectedValue);
        mock.allMessages().header(MY_HEADER).isEqualTo("testHeader");
        endpoint.sendBodyAndHeader("test", MY_HEADER, "testHeader");

        mock.assertIsSatisfied();
    }
}

希望有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM