eclipse - 第谷测试因 java.lang.NoClassDefFoundError : junit/framework/AssertionFailedError 失败

标签 eclipse maven junit junit4 tycho

我对此感到非常困惑和惊讶。根据我的理解,它应该有效,但我不知道为什么。

我有一个第谷版本,它只在 Eclipse 工作台中运行一些测试。然而,我的所有测试都因这种异常而失败:

java.lang.NoClassDefFoundError: junit/framework/AssertionFailedError
at org.grails.ide.eclipse.commands.test.AbstractCommandTest.tearDown(AbstractCommandTest.java:112)
at junit.framework.TestCase.runBare(TestCase.java:140)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.springsource.ide.eclipse.commons.tests.util.ManagedTestSuite.run(ManagedTestSuite.java:231)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
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:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:123)
at org.eclipse.tycho.surefire.osgibooter.OsgiSurefireBooter.run(OsgiSurefireBooter.java:84)
at org.eclipse.tycho.surefire.osgibooter.AbstractUITestApplication$1.run(AbstractUITestApplication.java:35)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:3529)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3182)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1022)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:916)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:585)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:540)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.tycho.surefire.osgibooter.UITestApplication.runApplication(UITestApplication.java:31)
at org.eclipse.tycho.surefire.osgibooter.AbstractUITestApplication.run(AbstractUITestApplication.java:114)
at org.eclipse.tycho.surefire.osgibooter.UITestApplication.start(UITestApplication.java:37)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
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.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)

Caused by: java.lang.ClassNotFoundException: junit.framework.AssertionFailedError
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 54 more

这对我来说没有意义。 org.junitorg.junit4 是运行测试的包以及包含 AbstractCommandTest 的包的依赖项。这些测试在我的工作区中运行时通过,那么为什么 maven/tycho 无法加载 junit 类呢?

如果有帮助的话,我很乐意提供更多信息,例如 github 存储库的链接。


编辑:提供更多详细信息

github 仓库在这里:https://github.com/grails/grails-sts-tests

您可以看到它是一个简单的单个插件,仅运行一些测试。测试套件由来自其他存储库中指定为依赖项的插件的测试组成。请注意,位于其他存储库中的测试插件在另一个 CI 服务器上运行,并且不存在 NoClassDefFoundError 问题。我查看了其他 CI 服务器,没有发现任何不同(不幸的是,该 CI 服务器不可公开访问,因此我无法共享指向它的链接)。

显示问题的 hudson 构建作业如下:http://hudson.grails.org/job/grails-sts-tests-2.0.x/17/console

最佳答案

我们遇到了非常相似的问题。原因是 junit 框架尝试创建和使用自定义类加载器,而这与 eclipse 插件类加载器不能很好地配合。我们现在遵循一些使用第谷运行单元测试的指南。

  1. 您的单元测试位于插件片段中。单元测试片段依赖于JUnit。
  2. 您的 pom 应该关闭单元测试的系统类加载器的使用。这就是导致 junit 测试类丢失的原因。
  3. 然后您使用与标准插件相同的包装(准确地说是 eclipse-plugin)。

所以一个简单的 POM(对于我们来说)如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <artifactId>master</artifactId>
    <groupId>mgws</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../../mgws/build-environment/pom.xml</relativePath>
  </parent>
  <groupId>mgws</groupId>
  <artifactId>mgws.spectral.test</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>eclipse-plugin</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12</version>
        <executions>
          <execution>
            <id>JUnitTest</id>
            <goals>
              <goal>test</goal>
            </goals>
            <phase>install</phase>
            <configuration>
              <useSystemClassLoader>false</useSystemClassLoader>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

这使得单元测试框架使用 eclipse 插件类加载器,现在它可以找到所有类。

关于eclipse - 第谷测试因 java.lang.NoClassDefFoundError : junit/framework/AssertionFailedError 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11369864/

相关文章:

java - Eclipse Maven 项目和 GIT

java - JUnit - 一次在@Before 中启动的字段在测试中为空

java - 想要使用 TestNg 包运行 Maven 项目,但在执行项目时出现错误

eclipse - 单元测试类在eclipse中运行旧版本

java - 无法使用 java eclipse 通过 lambda 函数将项目插入 dynamodb

java - Eclipse 中的 Json 错误指出预期值为 1 :0

linux - Arch Linux ARMv7 - 使用 Maven 进行 Pi4J 构建在 JNI 失败

java - Spring Boot应用程序部署在远程服务器上

java - eclipse 远程调试

javascript - 如何在 Eclipse 中编辑 javascript 内容辅助提案?