簡體   English   中英

用於Apache駱駝服務的Junit測試用例

[英]Junit Test Case for Apache camel service

我創建了apache camel rest service以從DB查詢數據

.get("/{id}/CodeId").description("get the CodeId").outType(String.class)
                .id("CodeId").param().name("codeid").type(RestParamType.path).description("Getting Id")
                .dataType("string").endParam().route()
                .to("sql:select * from table1 where C_ID=:#${header.codeid} ")
                .process(new Processor() {

                    @Override
                    public void process(Exchange ex) throws Exception {
                        String JId = null;
                        try {
                            List<HashMap> version = (List<HashMap>) ex.getIn().getBody();
                            if (version != null && version.size() > 0) {
                                LOG.debug(
                                        "jId------------------------------>" + version.get(0).get("J_ID"));
                                JId = (String) version.get(0).get("J_ID");
                            }

                            ex.getIn().setBody(JId, String.class);
                            ex.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 201);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).endRest();

實際上它工作正常,但我無法為該服務編寫junit測試用例。

誰能建議我一種方法來編寫Junit測試用例,以檢查服務中的值?

您可以使用任何http客戶端(例如camel-http)

class RestDemoTest extends CamelTestSupport {
  override def createRouteBuilder(): RouteBuilder = new RestDslRoutes()

  @Test
  def smokeTest(): Unit ={
    val body: String = template.requestBodyAndHeader("http://localhost:9898/demo/status","",Exchange.HTTP_METHOD, "GET", classOf[String])
    org.junit.Assert.assertThat(body,Is.is("Running"))
  }

}

示例代碼在scala中,但我敢肯定,您會明白的

編輯:根據要求獲取一個模擬的示例:

class RestDemoTest extends CamelTestSupport {
  override def createRouteBuilder(): RouteBuilder = new RestDslRoutes()

  // This example uses auto mocking endpoints based on a pattern
  override def isMockEndpointsAndSkip: String = "sql:*"

  @Test
  def mockDemo(): Unit = {
    // Obtain auto-created mock endpoint (false indicates: don't create if it does not exist yet. Helps with typos
    val mockEndpoint: MockEndpoint = getMockEndpoint("mock:sql:update foo set bar=10",false)
    // Set expectations/behaviour etc
    mockEndpoint.expectedMessageCount(1)

    val body: String = template.requestBody("http://localhost:9898/demo/update", "",  classOf[String])

    /// assert expectations/behaviour 
    mockEndpoint.assertIsSatisfied()
  }

實際上,我已經在此處記錄了有關單元測試用例的所有內容:

http://bushorn.com/unit-testing-apache-camel/

http://bushorn.com/camel-unit-testing-using-mock-endpoint/

在您的方案中,您需要做的是,必須用直接端點替換路由中來自端點的其余任何內容。 這樣,您可以從單元測試用例發送任何有效負載。 做這些與單元測試用例無關的協議始終是一個好習慣。 意思是,在您的單元案例中,不要涉及REST,JMS,STOMP或任何外部調用,那樣您就可以集中精力僅測試路由邏輯。 您仍然可以在集成測試中涵蓋協議級別的測試。

干杯。

暫無
暫無

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

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