从jar运行JUnit测试类别

时间:2019-05-08 10:43:37

标签: java gradle junit jar

此问题不仅涉及运行包含测试的jar,还涉及从jar内部运行测试类别

我们有一个测试类别,看起来像这样:

package com.company.test.suites;

import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;

import com.company.test.BasicSeleniumTests;

@RunWith(Categories.class)
@IncludeCategory(BasicSeleniumTests.class)
@SuiteClasses({ AllProjectTests.class })
public class RunBasicConfigurationSuite {
}

我们可以从Gradle启动它,我们已经在做,就像这样:

task functionalTests(type: Test, dependsOn: [ "testClasses" ]) {
    include 'com/company/test/suites/RunBasicConfigurationSuite.*'

    // Set JVM arguments for the test JVM(s).
    jvmArgs "-Xmx3600m", "-Xms1024m"
}

现在,我正在尝试为从命令行运行的工具创建一个独立的jar。我这样创建jar:

task testRunner(type: Jar) {
  manifest {
    attributes "Main-Class": "com.company.selenium.runners.TestRunner"
  }
  baseName = "TestRunner"
  from {
    configurations.testCompile.collect {
      it.isDirectory() ? it : zipTree(it)
    }
  }
  from sourceSets.test.output
  exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
  with jar
}

创建的jar看起来不错,我可以去

java -jar ../test-module/build/libs/TestRunner-4.1.0.0-SNAPSHOT.jar

然后jar启动并运行主类,如下所示:

打包com.company.selenium.runners;

导入org.junit.runner.JUnitCore;

public class TestRunner {

    public static void main(String[] args) {
        JUnitCore.main(
                "com.company.test.suites.RunBasicConfigurationSuite");
    }
}

但是,因为我们要运行测试类别,所以这不起作用。错误消息是:

1) initializationError0(com.company.test.suites.RunBasicConfigurationSuite)
java.lang.Exception: Custom runner class Categories should have a public constructor with signature Categories(Class testClass)

Categories是JUnit中的类,是的,它没有该构造函数。不确定我现在应该做什么?子类分类,并使用它吗?

编辑:那没有用。只是子类化和实现一个空的构造函数会导致以下结果:

java.lang.NoSuchMethodError: org.junit.runners.Suite.<init>(Ljava/lang/Class;Lorg/junit/runners/model/RunnerBuilder;)V

0 个答案:

没有答案