Search code examples
eclipseunit-testingjuniteclipse-pluginsnakeyaml

Problems with classloading when running JUnit plug-in tests in Eclipse


I have a problem when running JUnit Plug-in tests in Eclipse 4.3 and 4.2 and possible other versions. This worked in version 3.6 so I do not know what has changed.

The test are written using JUnit 4 and using parameterized tests (though I have tested with normal tests and the problem is the same). The test cases are written in YAML format and inflated at runtime using SnakeYAML.

When running the test as a normal JUnit test everything works fine, but when running the test as a JUnit Plug-in tests the test fails since it can no longer inflate objects from Yaml using SnakeYAML.

This is the code that reads test cases from 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 );
    }

}

And this is the stacktrace that is received when running as a JUnit Plug-in test

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

The strange thing (for me at least) is that I can make a StartsWithCommentTestCase object with

new StartsWithCommentTestCase()

without any problem also when running as a plugin test. In otherwords the class is avaiable at runtime, but for some reason not available to SnakeYAML.

Any pointers on this would be very helpful :)

Edit 1

The first lines of the YAML file:

#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

Edit 2

Added more code to the sample.


Solution

  • I found out that this is because I had moved the SnakeYAML dependency to another plugin. SnakeYAML was then part of another plugin with a different class loader than the test plugin.

    The solution was to add the following to the META-INF/MANIFEST.MF file in the dependeny plugin

    Eclipse-BuddyPolicy: registered
    

    And the following to the META-INF/MANIFEST.MF file in the test plugin

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

    Now on to see if I can move the SnakeYAML dependency back into a lib/ folder or similar.