使用 CamelBlueprintTestSupport 时的 NPE

NPE when using CamelBlueprintTestSupport

我想用 CamelBlueprintTestSupport 创建一个集成测试。 我开始我的 Camelcontext,起初看起来不错:

[ main] ingRestJobAdvertApiOfFirstbird INFO Skipping starting CamelContext as system property skipStartingCamelContext is set to be true. [ main] BlueprintCamelContext INFO Apache Camel 2.15.1.redhat-620133 (CamelContext: camel-1) is starting

路线也开始了。但后来我在我的控制台中收到了这条消息:

In main loop, we have serious trouble: java.lang.NullPointerException java.lang.NullPointerException at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:303)

骆驼版本:2.15.1.redhat-620133

单元测试:

public class WhenUsingRestJobAdvertApiOfdemo extends CamelBlueprintTestSupport {

@Override
protected String getBlueprintDescriptor() {
    return "OSGI-INF/blueprint/blueprint.xml";
}

@Override
protected String[] loadConfigAdminConfigurationFile() {
    return new String[]{"src/test/resources/jobwebsite.connector.properties", "jobwebsite.connector"};
}

@Test
public void testRoute() throws Exception {

    context.addRoutes(new MockServiceEndpoints());
    JobRequest jobRequest = readJoData();
    template.sendBody("direct:createjobIndemo", jobRequest);

    String expectedBody = "<matched/>";
    template.sendBodyAndHeader(expectedBody, "foo", "bar");
}

public  JobRequest readJoData() throws IOException {

    ObjectMapper mapper = new ObjectMapper();

    JobRequest jobRequest = mapper.readValue(new File("src/test/resources/demo-data/job-Advert/job-123.json"), JobRequest.class);

    return jobRequest;
}

}

经过进一步研究,我找到了解决方法。我必须阻止 bundle org.apache.felix.fileinstall 启动。这可以通过覆盖 CamelBlueprintTestSupport 中的 getBundleFilter() 来实现:

@Override
protected String getBundleFilter() {
    return "(!(Bundle-SymbolicName=org.apache.felix.fileinstall))";
}

我仍然不知道这是否是一般方法,或者我的项目设置是否有问题...

camel 中存在一个已知问题: https://issues.apache.org/jira/browse/CAMEL-7985

对我有用的解决方案发布在这里: https://issues.jboss.org/browse/ENTESB-2225。让我们在这里复制有用的评论:

When stepping up projects to 6.2 GA - my camel-test-blueprint unit tests all throw this NullPointerException excessively. I looked at the resolution of the bug report and applied that to my poms, and it cleared up all the NPEs. Here is what I did:

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-test-blueprint</artifactId>
        <scope>test</scope>
        <!-- Exclude in order to prevent -->
        <!-- java.lang.NullPointerException at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:303) -->
        <exclusions>
            <exclusion>
                <groupId>org.apache.felix</groupId>
                <artifactId>org.apache.felix.fileinstall</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Additionally, I had to add the following dependency:

<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.utils</artifactId>
    <scope>test</scope>
</dependency>