【问题标题】:Am I testing correctly?我测试正确吗?
【发布时间】:2013-10-21 19:46:03
【问题描述】:

我有一个与spring 集成的Camel 应用程序,我想为它编写测试。这是我的应用程序:

camel-config.xml

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <routeBuilder ref="converter" />
</camelContext>

<bean id="converter" class="Converter"/>

要测试的类:

@Component
public class Converter extends SpringRouteBuilder {

@Override
public void configure() throws Exception {

    final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
    xmlJsonFormat.setTypeHints(String.valueOf("YES"));

    from("ftp://Mike@localhost?" +
            "noop=true&binary=true&consumer.delay=5s&include=.*xml")
            .idempotentConsumer(header("CamelFileName"), FileIdempotentRepository.fileIdempotentRepository(new File("data", "repo.dat")))
            .marshal(xmlJsonFormat).to("file://data").process(
            new Processor() {
                //System.out.println();
            }
        });
   }
}

这是我的测试课:

public class RouteTest extends CamelTestSupport {

@Override
protected CamelContext createCamelContext() throws Exception {
    CamelContext context = super.createCamelContext();       
    context.addComponent("ftp", context.getComponent("seda"));
    return context;
}

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {                
            from("ftp://Mike@localhost").to("mock:quote");
        }
    };
}

@Test
public void testSameMessageArrived() throws Exception {
    MockEndpoint quote = getMockEndpoint("mock:quote");
    FileReader fl = new FileReader("D:\\test\\asdasd.txt");
    quote.expectedBodiesReceived(fl);
    template.sendBody("ftp://Mike@localhost", fl);
    quote.assertIsSatisfied();
}
}

此测试通过,但我不确定它是否是测试此特定程序的正确方法。
你能告诉我我做得对吗,或者我应该以其他方式测试它?

【问题讨论】:

    标签: java unit-testing apache-camel


    【解决方案1】:

    更好的方法是不重写路由。请改用您的实际路线。

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new Converter();
    }
    

    然后使用camel-mock,这样你就可以像这样拦截现有的端点:

    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            // mock all endpoints
            mockEndpoints();
        }
    });
    
    getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World");
    

    或带图案:

    context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            // mock only log endpoints
            mockEndpoints("log*");
        }
    });
    
    // now we can refer to log:foo as a mock and set our expectations
    getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World");
    

    如果您想了解更多有关使用 Camel 进行测试的信息,我强烈建议您阅读“Camel in Action”一书。

    编辑: 这是克劳斯易卜生对类似问题 (stack) 的回复。

    您可以使用专用或嵌入式 FTP 服务器进行集成测试,也可以使用模拟进行单元测试,具体取决于您要测试的内容。你也可以两者都做。

    【讨论】:

    • 我正在读这本书,但没有找到类似的例子。我还添加了@ContextConfiguration 注释。现在我可以看到我班级的测试覆盖率。还有一件事:我不应该与我的 ftp 服务器建立物理连接,是吗?我在expectedBodiesReceived 中输入了什么并不重要,我的测试总是通过。不应该是这样的,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多