【问题标题】:gwt-test-utils unit fail when run with jacoco与 jacoco 一起运行时,gwt-test-utils 单元失败
【发布时间】:2013-04-19 08:59:47
【问题描述】:

我们正在尝试从使用 gwt-test-utils 编写的一组单元测试中为我们的 GWT 应用程序生成覆盖率报告。该项目是一个多模块的Maven项目。我们在 jenkins 上使用声纳插件来生成和整理我们的覆盖范围和违规信息。

当构建作业运行时,所有 GWT 单元测试作为正常构建的一部分通过,但是当 Sonar 插件尝试重新运行测试时,它们都失败并出现以下错误:

initializationError(uk.co.card.gwt.retailpost.client.dialog.productmodify.CurrencyEditDialogTest) 已用时间:0 秒

查看 jenkins 控制台输出的其余部分和工作区目录,我找不到“com.google.gwt.core.ext.UnableToCompleteException:(请参阅以前的日志条目)”所指的日志文件到。

有没有人遇到过类似的问题并且知道如何让 Sonar 成功运行 gwt-test-utils,或者至少知道何时查找异常中提到的先前日志条目。

编辑:经过进一步实验,问题似乎是由 jacoco 引起的。尝试仅运行使用 jacoco 检测的单元测试(并且不涉及声纳)会导致相同的错误


**编辑:

来自 pom.xml 的示例

    <build>
pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.6.2.201302030002</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.12.4</version>
                    <configuration>                        
                        <excludedGroups combine.self="override" />
                        <reuseForks>true</reuseForks>
                        <argLine>-Xmx1024m -XX:MaxPermSize=256m ${jacoco.agent.argLine}</argLine>
                    </configuration>
                </plugin>
</plugins>
        </pluginManagement>
        <plugins>            
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <configuration>
                    <propertyName>jacoco.agent.argLine</propertyName>
                    <destFile>${sonar.jacoco.itReportPath}</destFile>
                    <append>true</append>
                    <excludes>
                        <exclude>*.osgi.*</exclude>
                        <exclude>*.apache.*</exclude>
                        <exclude>*.sourceforge.*</exclude>
                        <exclude>*.junit.*</exclude>
                        <!-- Test support code does not need to be covered -->
                        <exclude>uk.co.card.retailpost.clientintegration.utilities.*</exclude>
                    </excludes>
                    <classDumpDir>temp/classes</classDumpDir>
                </configuration>
                <executions>
                    <execution>
                        <id>agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>   
        </plugins>
    </build>

【问题讨论】:

  • 调试后发现问题出在maven + jacoco上。如果你从 Eclipse(ecl emma 插件)运行 jacoco - 你会看到代码覆盖率报告。
  • 我们需要将作业作为夜间 jenkins 构建作业的一部分运行,该作业使用声纳报告生成的覆盖率统计信息。知道我们如何生成覆盖范围,然后在该设置中的 maven/sonar 中重用它吗?由于 Eclipse 中的 org.apache.osgi 框架存在问题,该应用程序是在 netbeans 中构建的。

标签: sonarqube gwt-test-utils


【解决方案1】:

正如我在 cmets 中提到的,对于带有 maven-surefire-plugin 的 jacoco,库以不同的顺序加载。要解决此问题,请编写您自己的运行程序(扩展 com.googlecode.gwt.test.GwtRunner)并更改线程 contextClassLoader 的类加载器。

import com.googlecode.gwt.test.GwtRunner;

    public class MyGwtRunner extends GwtRunner {

        static {
            URLClassLoader classLoader = (URLClassLoader) MyGwtRunner.class.getClassLoader();

            try {
                URL[] urls = getClassPath();
                ClassLoader cl = URLClassLoader.newInstance(urls, classLoader);
                Thread.currentThread().setContextClassLoader(cl);
            } catch (MalformedURLException e) {
                throw new IllegalStateException(e);
            }

        }

        public MyGwtRunner(Class<?> clazz) throws Throwable {
            super(clazz);
        }

        private static URL[] getClassPath() throws MalformedURLException {
            String classPath = System.getProperty("java.class.path");
            String pathSeparator = System.getProperty("path.separator");
            String[] array = classPath.split(pathSeparator);

            List<URL> files = new ArrayList<URL>();
            for (String a : array) {
                files.add(new File(a).toURI().toURL());
            }
            return files.toArray(new URL[files.size()]);
        }

    }

在您的测试中,通过 MyGwtRunner 覆盖 GwtRunner

@GwtModule("com.my.module.GwtTestUtils")
@RunWith(MyGwtRunner.class)
public abstract class AbstractGwtJunit extends GwtTest { 
....
}

【讨论】:

  • 在使用此配置的声纳作业中仍然遇到同样的问题。但是,我会尝试让 jacoco 覆盖初始测试运行,并设置声纳以重用报告。一旦我尝试过,就会发表评论。
  • 我使用添加到上述问题中的 pom 设置对此进行了尝试,但仍然看到相同的错误。我不得不使用额外的 argline 参数让 jacoco 尝试生成 gwt-test-utils 单元测试的覆盖率。
  • 我只用詹金斯测试过。构建成功,但缺少报告...我会检查原因。
  • maven-surefire-plugin 使用最少的类路径运行测试。它运行 GwtRunner 类。但是 GWT 库尚未加载到类路径中。它会产生问题。我不知道,为什么只有当您使用 jacoco 运行 maven-surefire-plugin 时才会出现这种情况。如果您使用 Eclipse/手动中的 jacoco 一切正常。如果你在没有 jacoco 的情况下运行 maven-surefire-plugin 也可以正常工作。
  • 感谢您帮助调查此问题,我会看看是否无法使用此信息来制定解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多