编程语言
首页 > 编程语言> > java – Jenkins Cobertura build – NoClassDefFoundError

java – Jenkins Cobertura build – NoClassDefFoundError

作者:互联网

我有一个测试类ProcessorTest与行

JSONObject jsonObj = XML.toJSONObject(convert);
DBOb = (DBObject) JSON.parse(jsonObj.toString());

XML类来自json.org.该类在代码中的其他位置使用没有问题.

在localhost上运行正常(即所有Maven和JUnit测试都执行).

当我推到Jenkins时,我得到了这个错误:

jenkins错误

Error Details
net/sourceforge/cobertura/coveragedata/TouchCollector

Stack Trace
java.lang.NoClassDefFoundError: net/sourceforge/cobertura/coveragedata/TouchCollector
    at org.json.XML.__cobertura_init(XML.java)
    at org.json.XML.<clinit>(XML.java)
    at ProcessorTest.classSetup(ProcessorTest.java:81)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.cobertura.coveragedata.TouchCollector
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 26 more

我正在使用Jenkins 1.553和Cobertura插件1.9.6.

我该如何解决?

项目结构如下:

-- project1
     |
     | - core
     | - Tests (includes the tests that fail)


-- org.json
     |
     | - XML.class

当推送到Jenkins时,我假设org.json项目不可用于测试.

解决方法:

此异常和堆栈跟踪具有误导性;它实际上是因为你的类路径中没有包含SLF4J + ch.qos.logback(.ant / lib路径或ant可用的其他类路径). Cobertura(至少包括2.1.1,包括2.1.1)对ch.qos.logback SLF4J实现有特定要求.如果未包含此SLF4J实现,则可能导致以下堆栈跟踪:

java.lang.NoClassDefFoundError: net/sourceforge/cobertura/coveragedata/TouchCollector
    at [org.package.ClassName].__cobertura_init([ClassName].java)
    at [org.package.ClassName].<clinit>([ClassName].java)
    at [org.package.ClassName]Test.[method]([ClassName]Test.java:113)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.cobertura.coveragedata.TouchCollector
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

在我的情况下,我已经在我的.ant / lib类路径中使用了slf4j-simple-1.7.14.jar,我需要删除它并将其替换为logback-core-1.0.13.jarlogback-classic-1.0.13.jar.之后,我的检测测试用例在没有此异常的情况下执行.

标签:java,unit-testing,junit,jenkins,cobertura
来源: https://codeday.me/bug/20190708/1402501.html