JUnit测试随机失败,Camel 2.15.1 Custom TypeConverter未加载

时间:2015-04-16 15:19:44

标签: spring apache-camel junit4

JUnit测试随机失败,Camel 2.15.1自定义TypeConverter未加载。

我目前遇到自定义TypeConverter未加载的问题,单元测试随机失败。当我尝试多次运行它们时,有时会加载TypeConverter并且单元测试通过并按预期工作。有时找不到TypeConverter。

message=exception: org.apache.camel.InvalidPayloadException: No body available of type: org.apache.commons.httpclient.methods.RequestEntity.

我也定义了自定义TypeConverter。

@Converter
public final class MyTypeConverter {

@Converter
public static RequestEntity toRequestEntity(String string) {
    RequestEntity rtn = null;
    try {
        rtn = new StringRequestEntity(string, "application/x-www-form-urlencoded", "utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return rtn;
}
}

另外,添加了两个包/类,但行为相同。

META-INF/services/org/apache/camel/TypeConverter

 com.mycompany

尝试

com.mycompany.MyTypeConverter

我正在使用Spring 4.1.4.RELEASE  骆驼测试版2.15.1

这是我的骆驼路线

<camel:route id="send-to-xxxx">
        <camel:from uri="direct:xxxx"/>
        <camel:to uri="velocity:templates/xxxx-http-post.vm?loaderCache=false&amp;contentCache=false"/>
        <camel:setHeader headerName="http.requestMethod">
            <camel:constant>POST</camel:constant>
        </camel:setHeader>          
        <camel:convertBodyTo type="org.apache.commons.httpclient.methods.RequestEntity"/>
        <camel:onException>
            <camel:exception>java.lang.Exception</camel:exception>
            <camel:redeliveryPolicy maximumRedeliveries="0"/>
            <camel:handled>
                <camel:constant>true</camel:constant>
            </camel:handled>
            <camel:transform>
                <camel:simple>exception: ${exception.stacktrace} </camel:simple>
            </camel:transform>
            <camel:convertBodyTo type="com.xxx.ABC"/>
        </camel:onException>
        <camel:to ref="xxxx"/>
    </camel:route>

testcase扩展了一个加载了以下注释的类。

@RunWith(CamelSpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration

我试图在线寻找解决方案,但这些建议对我不起作用。

我已尝试按照线程中的建议将文件添加到classpath。并将包和类文件添加到META-INF / services / org / apache / camel / TypeConverter中的TypeConverter文件中,但测试仍然随机失败。

0 个答案:

没有答案