模拟Apache Camel在JUnit测试中的位置

时间:2018-01-02 11:44:34

标签: java spring-boot junit apache-camel

我正在尝试模仿Camel Routes的falsein路径,但我不知道如何提供模拟进出路径。请帮我修复此问题。谢谢提前

application.properties

out

application-test.properties

inputFilePath = src/main/resources/in
outputFilePath = src/main/resources/out

路由器和处理器:

inputFilePath = src/test/java/in
outputFilePath = src/test/java/out

Junit测试代码:

@Component
public class FileLineByLineRouter extends RouteBuilder {

    @Value("${inputFilePath}")
    private String inputFilePath;

    @Value("${outputFilePath}")
    private String outputFilePath;

    @Override
    public void configure() throws Exception {
        from("file://" + inputFilePath + "?delete=true").routeId("FileLineByLineRoute").marshal().string("UTF-8")
                .split(body().tokenize("\n")).streaming().process(getFileParsingProcessor())
                .to("file://" + outputFilePath + "?fileExist=Append").end();
    }

    @Bean
    public Processor getFileParsingProcessor() {

        return new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                String order = exchange.getIn().getBody(String.class);
                order = order + ": " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss S").format(new Date()) + "\n";
                exchange.getIn().setBody(order);
            }
        };
    }
}

日志:

    @RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@SpringBootTest(classes = FileLineByLineRouter.class)
@ActiveProfiles("test")
@EnableAutoConfiguration
public class FileLineByLineRouterTest2 extends CamelTestSupport {

    @Autowired
    protected CamelContext camelContext;

    @Test
    public void test() throws Exception {
        camelContext.start();
        Thread.sleep(2000);
        File outDir = new File("src/test/java/out");
        System.out.println(outDir.getAbsolutePath());
        assertTrue(outDir.isDirectory());
        assertTrue(outDir.listFiles().length != 0);
    }    
}

4 个答案:

答案 0 :(得分:3)

似乎你将Camel与Spring结合使用。我在这里使用的一般设置通常是我在其下面评论的一个用于解释所用概念有用的内容。

请注意,我还添加了一个暴露为Spring bean的通用服务,它实际上被定义为Mockito mock,以展示如何在测试中使用这些模拟。

// These 2 annotation allow the injection of Spring beans into this test class as 
// well, ideally if you want to mock certain services defined as Spring bean 
// with i.e. Mockito
@RunWith(CamelSpringRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
// we are going to slightly modify the route to test in order to simplify 
// things a bit, hence we use @UseAdviceWith
@UseAdviceWith
@ContextConfiguration(loader = AnnotationConfigContextLoader.class,
    classes = { FileLineByLineRouterTest2.ContextConfig.class })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class FileLineByLineRouterTest2 {

  // Spring bean setup needed to setup the beans required while testing your route

  @Configuration
  @PropertySource({"classpath:application-test.properties"})
  public static class ContextConfig extends CamelConfiguration {

    @Override
    public List<RouteBuilder> routes() {
      final List<RouteBuilder> routes = new ArrayList<>();
      routes.add(routeToTest());
      return routes;
    }

    // This bean is required to access the property files using @Value("${...}")
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public FileLineByLineRouter routeToTest() {
      return new FileLineByLineRouter();
    }

    @Bean
    public SomeService mockService() {
      return mock(SomeService.class);
    }
  }

  @Autowired
  private CamelContext camelContext;
  @Autowired
  private ProducerTemplate template;
  @Autowired
  private SomeService someService;
  // @MockEndpoints("mock:result")
  // private MockEndpoint resultEndpoint

  @Test
  public void test() throws Exception {

    // ARRANGE

    // modify the route to test to simplify testing
    camelContext.getRouteDefinition("FileLineByLineRoute")
        .adviceWith((ModelCamelContext) camelContext, 
            new  AdviceWithRouteBuilder() {
                @Override
                public void configure() throws Exception {
                    // replace the file endpoint used in the from RouteBuilder
                    // method with a direct endpoint for easier testing
                    this.replaceFromWith("direct:start");
                    // redirect the output which should be written to a file
                    // to a mock endpoint
                    this.interceptSendToEndpoint("file:*")
                        .skipSendToOriginalEndpoint()
                        .to("mock:result");
                }
            });

    // create a mock endpoint to redirect output and define some basic assertions 
    // on either the number of processed outputs or the format of the body 
    // received by that mock endpoint. You can also use the commented out 
    // annotation approach from above instead
    // More information available at: http://camel.apache.org/mock.html
    MockEndpoint resultEndpoint = camelContext.getEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedMessageCount(1);
    // other assertions on the mock endpoint possible as well here

    // define behavior of mocks
    when(someService.doSomething()).thenReturn("something");

    // ACT

    // use the producer template to send a body (and headers) to the route 
    // to test. This can litteraly be anything. A byte array, a simple string, 
    // a complex object, ...
    String testContent = "...";
    template.sendBody("direct:start", testContent);

    // ASSERT

    // check that the mock endpoint has received the actual number of bodies specified
    // before invoking the template above
    resultEndpoint.assertIsSatisfied();

    // retrieve the final body and/or headers from the processed exchanges
    // and perform your assertion against them
    List<Exchange> exchanges = resultEndpoint.getExchanges();
    assertThat(exchanges.size(), is(equalTo(1));
    Object retBody = exchanges.get(1).getOut().getBody();
    assertThat(retBody, is(equalTo(...));
  }
}

如果您确实希望将文件使用者和生产者保留在您的路由中进行测试,我会使用JUnits TemporaryFolder规则,而不是看起来像这样:

private MockEndpoint result;

@Rule
public TemporaryFolder sourceFolder = new TemporaryFolder();

@Before
public void init() throws Exception {
  result = context.getEndpoint("mock:result", MockEndpoint.class);

  context.getRouteDefinition("route-to-test")
    .adviceWith((ModelCamelContext) context,
                new AdviceWithRouteBuilder() {
                  @Override
                  public void configure() throws Exception {
                    replaceFromWith("file://" + sourceFolder.getRoot().toString()
                                    +"?fileExist=Move"
                                    + "&moveExisting=${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.${file:ext}"
                                    + "&tempFileName=${file:name}.tmp");
                    interceptSendToEndpoint("file:*").skipSendToOriginalEndpoint().to(result);
                  }
                });

  writeFileContent("sample1.xml", "/filesystem/outbound/sample1.xml");
  writeFileContent("sample2.xml", "/filesystem/outbound/sample2.xml");
  writeFileContent("sample3.xml", "/filesystem/outbound/sample3.xml");
  writeFileContent("sample4.xml", "/filesystem/outbound/sample4.xml");

  context.start();
}

其中writeFileContent简单地将文件内容复制到用于测试的临时文件夹

private void writeFileContent(String name, String source) throws Exception {
  File sample = sourceFolder.newFile(name);
  byte[] bytes = IOUtils.toByteArray(getClass().getResourceAsStream(source));
  FileUtils.writeByteArrayToFile(sample, bytes);
}

输出实际上也可以写入临时测试目录,而不是通过模拟端点处理它。这种方法类似于定义用于将文件发送到路由的临时测试目录。因此,我将这种方法留给你。

答案 1 :(得分:2)

好的,在重新阅读您的评论和更新的问题之后,我想我现在明白你的意思了...你的测试还没有完成。

试试这个:

  • 删除Testclass中的extends CamelTestSupport。这是基于注释的测试支持的替代方法。
  • 删除测试中的camelContext.start()。我可能会把你和我的建议例子搞糊涂了。您只需在使用@UseAdviceWith
  • 注释班级时自己启动上下文
  • 最后,等一下。为了示例,在测试中插入Thread.sleep(10000)以便为文件提供处理时间。

您可以使用Camel NotifyBuilder(http://camel.apache.org/notifybuilder.html

代替固定睡眠

答案 2 :(得分:1)

您可以将inout路径提取到application-dev.properties(或yml)。

path.in=src/main/resources/in
path.out=src/main/resources/out

然后configure()方法应该改为

@Override
public void configure() throws Exception {
    from("file://{{path.in}}?delete=true")
    .routeId("FileLineByLineRoute")
    .marshal().string("UTF-8")
    .split(body().tokenize("\n")).streaming()
    .process(getFileParsingProcessor())
    .to("file://{{path.out}}?fileExist=Append")
    .end();
}

然后在测试中,您可以模拟属性,也可以加载不同的属性文件

答案 3 :(得分:0)

我想你可以在这里找到答案:sample FilterTest.java

以下是相关的摘录。

@Override
protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        public void configure() {
          from("direct:start").filter(header("foo").isEqualTo("bar"))
          .to("mock:result");
        }
    };
}