3

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

@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");
        }
    };
  }
}

处理器:

@Component
public class CamelS3HeadersProcessor implements Processor {
  @Override
  public void process(Exchange exchange) throws Exception {
    SimpleBuilder simpleBuilder = new SimpleBuilder("${file:name}");
    String fileName = simpleBuilder.evaluate(exchange, String.class);
    //do some logic and set headers
  }
}

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

问题:

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

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

4

2 回答 2

3

我认为在 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 {
  }
}
于 2016-12-07T09:21:21.093 回答
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);
    }

}

(另请注意,我已从路线中删除了模拟端点:请参阅此问题如何在不更改生产代码的情况下测试骆驼路线?了解如何在没有它的情况下进行测试。)

于 2017-04-25T13:09:37.037 回答