在buildSrc目录中运行测试用例的问题

时间:2019-02-12 11:26:27

标签: unit-testing gradle

我有一个执行Caesar Cipher加密的自定义Java类,我想对其进行单元测试。但是问题是当我尝试对其进行测试时,IDE显示错误:

  

java.lang.Throwable:无法确定要执行的gradle任务       在com.intellij.openapi.diagnostic.Logger.error(Logger.java:126)       在com.android.tools.idea.gradle.run.DefaultGradleBuilder.build(DefaultGradleBuilder.java:42)       在com.android.tools.idea.gradle.run.MakeBeforeRunTaskProvider.doExecuteTask(MakeBeforeRunTaskProvider.java:360)       在com.android.tools.idea.gradle.run.MakeBeforeRunTaskProvider.executeTask(MakeBeforeRunTaskProvider.java:268)       在com.android.tools.idea.gradle.run.MakeBeforeRunTaskProvider.executeTask(MakeBeforeRunTaskProvider.java:122)       在com.intellij.execution.impl.ExecutionManagerImpl.lambda $ compileAndRun $ 0(ExecutionManagerImpl.java:298)       在com.intellij.openapi.application.impl.ApplicationImpl $ 1.run(ApplicationImpl.java:314)       在java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)       在java.util.concurrent.FutureTask.run(FutureTask.java:266)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       在java.lang.Thread.run(Thread.java:745)

这是我build.gradle目录的buildSrc文件内容:

plugins {
    id 'java'
}

sourceSets {
    main {
        groovy {
            srcDir 'src/main/groovy'
        }
        java {
            srcDir 'src/main/java'
        }
    }
    test {
        java {
            srcDir 'src/test/java'
        }
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation localGroovy()
    implementation gradleApi()
    implementation "commons-codec:commons-codec:$codec_version"
    testImplementation "junit:junit:$junit_version"
}

P.S .:当我尝试使用-Dtest.single=package.*从命令行运行测试类时,找不到该类。

1 个答案:

答案 0 :(得分:0)

很不幸,这是一个WONTFIX错误,这里https://issues.gradle.org/browse/GRADLE-1553

作为解决方法(已发布),您可以使用--tests,即:

# Executes all tests in SomeTestClass
gradle test --tests SomeTestClass

# Executes a single specified test in SomeTestClass
gradle test --tests SomeTestClass.someSpecificMethod

gradle test --tests SomeTestClass.*someMethod*

此处记录: https://docs.gradle.org/current/userguide/java_testing.html#simple_name_pattern