【问题标题】:Problems with classloading when running JUnit plug-in tests in Eclipse在 Eclipse 中运行 JUnit 插件测试时的类加载问题
【发布时间】:2013-12-26 21:10:31
【问题描述】:

在 Eclipse 4.3 和 4.2 以及可能的其他版本中运行 JUnit 插件测试时遇到问题。这在 3.6 版中有效,所以我不知道发生了什么变化。

测试是使用 JUnit 4 并使用参数化测试编写的(尽管我已经使用普通测试进行了测试并且问题是相同的)。测试用例以 YAML 格式编写,并在运行时使用 SnakeYAML 进行扩展。

当作为普通 JUnit 测试运行测试时,一切正常,但当作为 JUnit 插件测试运行测试时,测试失败,因为它不能再使用 SnakeYAML 从 Yaml 中膨胀对象。

这是从 YAML 读取测试用例的代码

public static ArrayList<StartsWithCommentTestCase> readTests(String filename) {

    // set up how Java types match to the YAML file
    Constructor testCaseConstructor = new Constructor(StartsWithCommentTestCase.class);

    TypeDescription testCaseDesc = new TypeDescription(StartsWithCommentTestCase.class);
    testCaseConstructor.addTypeDescription(testCaseDesc);

    Yaml yamlParser = new Yaml(testCaseConstructor);

    // read all the documents in the test file
    String tests = TestUtils.readFile(filename);
    ArrayList<StartsWithCommentTestCase> testCases = new ArrayList<StartsWithCommentTestCase>();
    for (Object testCase : yamlParser.loadAll(new StringReader(tests))) {
        testCases.add((StartsWithCommentTestCase) testCase);
    }

    return testCases;

}

@Test
public void bla(){
    List<StartsWithCommentTestCase> tests = readTests(TEST_FILES_DIR + "starts-with-comment.yaml");
    for(StartsWithCommentTestCase test : tests){
        boolean actual = ToggleCommentHandler.startsWithComment( test.getLine() );
        assertEquals( test.getName(), test.getExpected(), actual );
    }

}

这是作为 JUnit 插件测试运行时收到的堆栈跟踪

Can't construct a java object for tag:yaml.org,2002:pde.test.tests.StartsWithCommentTestCase;exception=Class not found: pde.test.tests.StartsWithCommentTestCase
 in "<reader>", line 4, column 1:
   name: Empty line
   ^
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:325)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:181)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:140)
at org.yaml.snakeyaml.constructor.BaseConstructor.getData(BaseConstructor.java:109)
at org.yaml.snakeyaml.Yaml$1.next(Yaml.java:317)
at pde.test.tests.StartsWithCommentTest.readTests(StartsWithCommentTest.java:86)
at pde.test.tests.StartsWithCommentTest.bla(StartsWithCommentTest.java:47)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.pde.internal.junit.runtime.RemotePluginTestRunner.main(RemotePluginTestRunner.java:62)
at org.eclipse.pde.internal.junit.runtime.CoreTestApplication.run(CoreTestApplication.java:23)
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.internal.app.EclipseAppContainer.callMethodWithException(EclipseAppContainer.java:587)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:198)
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:354)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:181)
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:636)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:591)
at org.eclipse.equinox.launcher.Main.run(Main.java:1450)
at org.eclipse.equinox.launcher.Main.main(Main.java:1426)
Caused by: org.yaml.snakeyaml.error.YAMLException: Class not found: pde.test.tests.StartsWithCommentTestCase
at org.yaml.snakeyaml.constructor.Constructor.getClassForNode(Constructor.java:625)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.getConstructor(Constructor.java:313)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:323)
... 49 more

奇怪的是(至少对我来说)我可以用

创建一个 StartsWithCommentTestCase 对象
new StartsWithCommentTestCase()

作为插件测试运行时也没有任何问题。换句话说,该类在运行时可用,但由于某种原因对 SnakeYAML 不可用。

对此的任何指示都会非常有帮助:)

编辑 1

YAML 文件的第一行:

#all the lines must have newlines in them since newlines are returned
#by Document.get()
---
name: Empty line
line:
expected: false
---
name: "Line where first character is #"
line: "#

"
expected: true

编辑 2

向示例中添加了更多代码。

【问题讨论】:

    标签: eclipse unit-testing junit eclipse-plugin snakeyaml


    【解决方案1】:

    Junit 插件似乎不知道如何读取 SnakeYAML 代码。当您通过插件运行测试时,您确定 SnakeYAML 的类加载器已正确初始化?

    提到的第 4 行在您的 YAML 代码中看起来如何?是空的吗?

    也许 junit 插件使用了不同于普通设置的其他 jar 依赖项。 junit 插件使用哪个 Maven 二进制文件/路径?这和你普通的一样吗?如果你有不同的 m2 配置,甚至不同的 repos,就会出现奇怪的问题。

    【讨论】:

    • 类加载器很可能发生了一些有趣的事情。所以我的猜测是,不,它没有正确初始化。
    • 我添加了部分 YAML 文件。这里没有使用 Maven 配置。仅使用 Eclipse、OSGi 捆绑软件。
    • 什么是 pde.test.tests.StartsWithCommentTest.bla(StartsWithCommentTest.java:47)?
    • 我现在已将 bla() 方法添加到代码示例中。 runTests() 本来是一个更好的名字,但我有点累了:)
    【解决方案2】:

    我发现这是因为我已将 SnakeYAML 依赖项移至另一个插件。 SnakeYAML 然后是另一个插件的一部分,其类加载器与测试插件不同。

    解决方案是将以下内容添加到依赖插件中的 META-INF/MANIFEST.MF 文件中

    Eclipse-BuddyPolicy: registered
    

    测试插件中的META-INF/MANIFEST.MF文件如下

    Eclipse-RegisterBuddy: <symbolic name of the dependency plugin>
    

    现在看看我是否可以将 SnakeYAML 依赖项移回 lib/ 文件夹或类似文件夹。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 2013-10-06
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多