Camel Testing - 如何从FileConsumer / FTPConsumer测试处理器?

时间:2016-11-21 06:00:13

标签: java junit apache-camel mockito camel-test

我有一个camel路由,它从ftp服务器轮询文件并将文件发送到s3。我在路由中有一些处理器根据文件名计算/操作头。我需要测试这条路线,如何注入处理器并在处理器中使用文件语言?

<div>
    <textarea class="yyyyyy232" id="scripttextarea_1"></textarea>
    <button type="submit" class='yy4e' id='btn1'>Save1</button>
</div>

处理器:

@RunWith(MockitoJUnitRunner.class)
public class CamelS3HeadersProcessorTest extends CamelTestSupport {
  private String filePath = "src/test/resources/sample.txt";

 // @Autowired
//  private CamelS3HeadersProcessor camelS3HeadersProcessor;

  @Test
  public void shouldSetS3HeadersProperly() throws Exception {
    File file = new File(filePath);
    template.sendBody("direct:start", file);
    getMockEndpoint("mock:result").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedHeaderReceived(S3Constants.KEY, file.getName());
    assertMockEndpointsSatisfied();
  }

  @Override
  protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").process(new CamelS3HeadersProcessor()).to("mock:result");
        }
    };
  }
}

我不想嘲笑我的处理器。我想模仿我的端点并测试我的处理器。

问题:

  1. 无法自动加入/注入我的处理器。

  2. 文件名被评估为null。如何使用FileConsumer / FTPConsumer而不是ProducerTemplate?

2 个答案:

答案 0 :(得分:1)

我认为在createRouteBuilder中你只有CamelContext而不是ApplicationContext。您可以在测试中或@Before方法中构建路径。

这对我有用:

@SpringBootTest
@EnableAutoConfiguration
@RunWith(CamelSpringRunner.class)
public class MyProcessorTest extends AbstractJUnit4SpringContextTests {

  @Autowired
  private CamelContext camelContext;

  @Autowired
  private MyProcessor myProcessor;

  @EndpointInject(uri = "mock:result")
  private MockEndpoint resultEndpoint;

  @Produce(uri = "direct:start")
  private ProducerTemplate template;

  @Before
  public void setUp() throws Exception {
    camelContext.addRoutes(new RouteBuilder() {
      @Override
      public void configure() throws Exception {
        from("direct:start").process(myProcessor).to("mock:result");
      }
    });
    camelContext.start();
  }

  @DirtiesContext
  @Test
  public void test1() throws Exception {
    resultEndpoint.expectedMessageCount(1);
    String myProcessorInput = "test"; // or Object
    template.sendBody(myProcessorInput);

    resultEndpoint.assertIsSatisfied();

    // or other result Type
    String body = (String)resultEndpoint.getExchanges().get(0).getIn().getBody(String.class);

    assertNotNull(body);
    // tests
  }

  @DirtiesContext
  @Test
  public void test2() throws Exception {
  }
}

答案 1 :(得分:0)

在生产代码中,您的RouteBuilder将在测试之外创建,因此可以注入您选择的生产者,例如

@Component
class MyRouteBuilder extends RouteBuilder {

    private Processor processor;

    @Autowired
    public MyRouteBuilder(Processor pProcessor) {
        processor = pProcessor;
    }

    @Override
    public void configure() {
        from("direct:start").process(processor);
    }
}

然后在您的测试中,您可以提供您想要的任何处理器(模拟或非模拟):

public class MyTest extends CamelTestSupport {

    private Processor processor;

    @Override
    protected RoutesBuilder createRouteBuilder() {
        return new MyRouteBuilder(processor);
    }

}

(另请注意,我已从路线中删除了模拟端点:请参阅此问题How do I test a camel route without changing the production code?了解如何在没有它的情况下进行测试。)