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

在Eclipse中运行JUnit插件测试时的类加载问题,eclipse,unit-testing,junit,eclipse-plugin,snakeyaml,Eclipse,Unit Testing,Junit,Eclipse Plugin,Snakeyaml,在Eclipse4.3和4.2以及可能的其他版本中运行JUnit插件测试时,我遇到了一个问题。这在版本3.6中起作用,所以我不知道发生了什么变化 该测试是使用JUnit4和参数化测试编写的(尽管我已经用普通测试进行了测试,问题也是一样的)。测试用例以YAML格式编写,并在运行时使用SnakeYAML进行膨胀 当作为正常的JUnit测试运行测试时,一切正常,但当作为JUnit插件测试运行测试时,测试失败,因为它不能再使用SnakeYAML从Yaml中膨胀对象 这是从YAML读取测试用例的代码 pu

在Eclipse4.3和4.2以及可能的其他版本中运行JUnit插件测试时,我遇到了一个问题。这在版本3.6中起作用,所以我不知道发生了什么变化

该测试是使用JUnit4和参数化测试编写的(尽管我已经用普通测试进行了测试,问题也是一样的)。测试用例以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 );
    }

}
作为插件测试运行时也没有任何问题。换句话说,该类在运行时可用,但由于某些原因,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


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

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

提到的第4行在YAML代码中是什么样子的?是空的吗


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

我发现这是因为我将SnakeYAML依赖项移到了另一个插件上。SnakeYAML是另一个插件的一部分,它的类装入器与测试插件不同

解决方案是在dependeny插件的META-INF/MANIFEST.MF文件中添加以下内容

Eclipse-BuddyPolicy: registered
下面是测试插件中的META-INF/MANIFEST.MF文件

Eclipse-RegisterBuddy: <symbolic name of the dependency plugin>
Eclipse注册表预算:

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

类加载器中可能有一些有趣的东西。所以我的猜测是,没有正确初始化。我添加了YAML文件的一部分。在此过程中没有使用Maven配置。只使用Eclipse、OSGi捆绑包的东西。什么是pde.test.tests.StartsWithCommentTest.bla(StartsWithCommentTest.java:47)?我现在已经在代码示例中添加了bla()方法。runTests()应该是个更好的名字,但我有点累了:)
Eclipse-BuddyPolicy: registered
Eclipse-RegisterBuddy: <symbolic name of the dependency plugin>