为什么在我的Jenkins-Sonar插件中测试失败但在Jenkins-Maven运行期间没有?

时间:2012-10-10 18:36:42

标签: maven junit jenkins classpath sonarqube

我在CentOS上运行Jenkins 1.478,使用Java 6,Maven 3.0.4,JUnit 4.8.1(Maven项目中的依赖项)和Sonar 3.2.1。我有我的Jenkins Maven 2/3作业设置在完成后运行Sonar(设置目标为“clean package -Pdev”)。该项目是一个包含WAR和EAR模块的多模块项目。但是,当插件的Sonar部分运行时,许多测试会因下面的错误而死...

java.lang.NullPointerException
        at java.util.Properties$LineReader.readLine(Properties.java:418)
        at java.util.Properties.load0(Properties.java:337)
        at java.util.Properties.load(Properties.java:325)
        at org.parentco.myco.client.test.AbstractHibernateDaoTest.loadmyprojectProps(AbstractHibernateDaoTest.java:252)
        at org.parentco.myco.client.test.AbstractHibernateDaoTest.setupMockEjbContainer(AbstractHibernateDaoTest.java:235)
        at org.parentco.myco.client.test.AbstractHibernateDaoTest.setupBeforeClass(AbstractHibernateDaoTest.java:72)
        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:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

异常来自此代码块的最后一行......

    final InputStream in = AbstractHibernateDaoTest.class.getClassLoader().getResourceAsStream("myproject.properties");
    final Properties props = new Properties();
    props.load(in);

测试在Maven部分工作期间完美运行。有问题的文件位于

./myclient-war/src/main/resources/myproject.properties

任何人都知道如何进一步解决这个问题?我更愿意在Sonar中配置一些东西,而不是为了适应Sonar重组我的整个项目,但我愿意接受建议。

3 个答案:

答案 0 :(得分:1)

这可能是因为Sonar为运行每个测试创建了一个单独的类加载器,尽管我不太确定。

我相信所有测试都没有遵循等级制度?

  

YourTestClass扩展了AbstractHibernateDaoTest

尝试使用即时测试类YourTestClass的类加载器加载文件,而不是总是尝试使用AbstractHibernateDaoTest

的类加载器加载它

尝试将代码更改为

final InputStream in = this.getClass().getClassLoader()
                        .getResourceAsStream("myproject.properties");
final Properties props = new Properties();
props.load(in);

这将确保您获得实际测试类的类对象而不是抽象类,以及直接类的类加载器而不是已加载的类加载器。

<强>更新 一世 我觉得发生的事情是,无论在代码中使用什么类加载器来加载资源,都无法看到Maven的类加载器。请注意,在您的情况下,Maven和特别是maven-surefire-plugin负责设置正确的类路径。它将在类路径中添加src/main/resources目录。

答案 1 :(得分:0)

尝试“/myproject.properties”。

ClassLoader cl = AbstractHibernateDaoTest.class.getClassLoader();  
InputStream inTmp = cl.getResourceAsStream("myproject.properties");
if(itTmp==null){
    itTmp=cl.getResourceAsStream("/myproject.properties");
}
final InputStream in = inTmp;

答案 2 :(得分:0)

一种可能性是this issue

基于Spring的junit测试与Sonar使用的Cobertura插件冲突。解决方法是使用JaCoCo或EMMA插件而不是Cobertura。