4

ClassNotFoundException在 Pax Exam 测试方法中使用匿名类时,我得到了一个。

我的测试课:

package com.bssys.ebpp.paxexam;

import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.*;

import org.apache.camel.Processor;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.component.jdbc.JdbcComponent;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.ProbeBuilder;
import org.ops4j.pax.exam.TestProbeBuilder;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.karaf.options.LogLevelOption;
import org.ops4j.pax.exam.util.Filter;
import org.osgi.framework.Constants;

import javax.inject.Inject;
import javax.sql.DataSource;
import javax.sql.XADataSource;
import java.io.File;

/**
 * @author zhupv
 */
@RunWith(PaxExam.class)
public class TransactionsTest {

    @Inject
    @Filter(timeout = 30000)
    Transactions transactions;

    @Inject
    DataSource dataSource;

    @Configuration
    public Option[] config() {

        return new Option[]{
                karafDistributionConfiguration()
                        .frameworkUrl(
                                maven()
                                        .groupId("org.apache.servicemix")
                                        .artifactId("apache-servicemix")
                                        .type("zip")
                                        .version("6.0.0.M2")
                        )
                        .karafVersion("3.0.3")
                        .name("Apache ServiceMix")
                        .unpackDirectory(new File("target/pax"))
                        .useDeployFolder(false),
                keepRuntimeFolder(),
                debugConfiguration("5005", true),
                logLevel(LogLevelOption.LogLevel.DEBUG),

                features(
                        maven().groupId("org.apache.karaf.features").artifactId("enterprise").version("3.0.3").type("xml").classifier("features"),
                        "transaction"
                ),
                features(
                        maven().groupId("org.apache.camel.karaf").artifactId("apache-camel").version("2.15.2").type("xml").classifier("features"),
                        "camel", "camel-blueprint", "camel-core", "camel-spring", "camel-jdbc"
                ),
                mavenBundle()
                        .groupId("com.h2database")
                        .artifactId("h2")
                        .version("1.4.187")
                        .start(),
                mavenBundle()
                        .groupId("com.bssys.ebpp")
                        .artifactId("sandbox-services")
                        .version("1.0.0")
                        .start()
        };
    }

    @ProbeBuilder
    public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
        System.out.println("TestProbeBuilder gets called");
        probe.setHeader(Constants.DYNAMICIMPORT_PACKAGE, "*");
        return probe;
    }

    @Test
    public void test() throws Exception{
        try{
            SimpleRegistry sr = new SimpleRegistry();
            sr.put("ds", dataSource);
            CamelContext camelContext = new DefaultCamelContext(sr);
            camelContext.addComponent("jdbc", new JdbcComponent());
            ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
            producerTemplate.sendBody(
                    "jdbc://ds",
                    "create table if not exists test_table(col1 varchar(255))"
            );
            Endpoint endpoint = camelContext.getEndpoint("jdbc://ds");
            Exchange exchange = endpoint.createExchange();
            exchange.getIn().setBody("select count(*) from test_table");
            Exchange out = producerTemplate.send(endpoint, exchange);

            out = producerTemplate.send(
                    "jdbc://ds",
                    new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            exchange.getIn().setBody(
                                    "select count(*) from test_table"
                            );
                        }
                    }
            );

            transactions.doWork();
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }
}

堆栈跟踪:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_25]
    at org.ops4j.pax.exam.rbc.internal.RemoteBundleContextImpl.remoteCall(RemoteBundleContextImpl.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_25]
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)[:1.7.0_25]
    at sun.rmi.transport.Transport$1.run(Transport.java:177)[:1.7.0_25]
    at sun.rmi.transport.Transport$1.run(Transport.java:174)[:1.7.0_25]
    at java.security.AccessController.doPrivileged(Native Method)[:1.7.0_25]
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)[:1.7.0_25]
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)[:1.7.0_25]
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)[:1.7.0_25]
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)[:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[:1.7.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)[:1.7.0_25]
    at java.lang.Thread.run(Thread.java:724)[:1.7.0_25]
Caused by: org.ops4j.pax.exam.TestContainerException: [test(com.bssys.ebpp.paxexam.TransactionsTest): com/bssys/ebpp/paxexam/TransactionsTest$1]
    at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.createTestContainerException(JUnitProbeInvoker.java:138)
    at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.invokeViaJUnit(JUnitProbeInvoker.java:127)
    at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.findAndInvoke(JUnitProbeInvoker.java:97)
    at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.call(JUnitProbeInvoker.java:73)
    ... 20 more
Caused by: java.lang.NoClassDefFoundError: com/bssys/ebpp/paxexam/TransactionsTest$1
    at com.bssys.ebpp.paxexam.TransactionsTest.test(TransactionsTest.java:111)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.7.0_25]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)[:1.7.0_25]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.7.0_25]
    at java.lang.reflect.Method.invoke(Method.java:606)[:1.7.0_25]
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.ops4j.pax.exam.invoker.junit.internal.ContainerTestRunner.runChild(ContainerTestRunner.java:68)
    at org.ops4j.pax.exam.invoker.junit.internal.ContainerTestRunner.runChild(ContainerTestRunner.java:37)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.ops4j.pax.exam.invoker.junit.internal.JUnitProbeInvoker.invokeViaJUnit(JUnitProbeInvoker.java:124)
    ... 22 more
Caused by: java.lang.ClassNotFoundException: com.bssys.ebpp.paxexam.TransactionsTest$1 not found by sandbox-services [159]
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.7.0_25]
    at org.apache.felix.framework.BundleWiringImpl.getClassByDelegation(BundleWiringImpl.java:1374)
    at org.apache.felix.framework.BundleWiringImpl.searchImports(BundleWiringImpl.java:1553)
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1484)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.7.0_25]
    ... 44 more

当探针执行以下代码时会引发异常:

out = producerTemplate.send(
                    "jdbc://ds",
                    new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            exchange.getIn().setBody(
                                    "select count(*) from test_table"
                            );
                        }
                    }
            );

有人知道这背后的原因是什么吗?

编辑:我正在使用 Pax Exam 4.5.0

4

1 回答 1

4

根本原因是ClassNotFoundException捆绑包中的沙盒服务,它试图从您的考试探针捆绑包中加载测试类。这会失败,因为默认情况下,探针不会导出任何包。添加一个 Export-Package 标头TestProbeBuilder应该可以使这项工作:

@ProbeBuilder
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) {
    System.out.println("TestProbeBuilder gets called");
    probe.setHeader(Constants.DYNAMICIMPORT_PACKAGE, "*");
    probe.setHeader(Constants.EXPORT_PACKAGE, "com.bssys.ebpp.paxexam");
    return probe;
}
于 2015-07-01T11:25:28.807 回答