在Eclipse中运行JUnit插件测试时的类加载问题

时间:2013-12-26 21:10:31

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

在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

为示例添加了更多代码。

2 个答案:

答案 0 :(得分:0)

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

上面提到的第4行如何查看您的YAML代码?它是空的吗?

也许junit插件使用的是另一个jar依赖,而不是普通的设置。 junit插件使用哪个maven二进制/路径?这和普通人一样吗?如果您有不同的m2配置甚至不同的回购,那么会出现奇怪的问题。

答案 1 :(得分:0)

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

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

Eclipse-BuddyPolicy: registered

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

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

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