JUnit Tests fail's ramdomly, Camel 2.15.1 Cust

2019-09-18 16:19发布

问题:

JUnit Tests fail's randomly, Camel 2.15.1 Custom TypeConverter not getting loaded.

I am currently having issues with custom TypeConverter not getting loaded, Unit tests are failing randomly. When I am trying to run them multiple times, sometimes the TypeConverter gets loaded and Unit Test pass and work as expected. Sometimes the TypeConverter is not found.

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

Also I have defined my custom 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;
}
}

Also, Have added the package/class tried both but the behavior is same.

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

 com.mycompany

Tried

com.mycompany.MyTypeConverter

I am using Spring 4.1.4.RELEASE Camel-test version 2.15.1

This is my camel route

<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>

The testcase extends a class which is loaded with following annotations.

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

I have tried to look for solution online, but the recommendations didn't work for me.

I have tried adding the file to classpath as recommended in a thread. And Adding the package and class file both to the TypeConverter file in META-INF/services/org/apache/camel/TypeConverterbut still the tests fail randomly.