Вы можете использовать конфигурацию из официальных junit5-samples . Там вы можете найти проект junit5-jupiter-starter-gradle-groovy
, который использует следующую конфигурацию:
plugins {
id 'groovy'
id 'eclipse' // optional (to generate Eclipse project files)
id 'idea' // optional (to generate IntelliJ IDEA project files)
}
repositories {
mavenCentral()
}
dependencies {
implementation(localGroovy())
testImplementation('org.junit.jupiter:junit-jupiter:5.6.0')
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
}
Он работает с Gradle 6.0.1, Java 8 и 11.
Для запуска тестов вы можете использовать следующую команду: gradle test
или настроить в вашей IDE, как показано:
Вы увидите что-то вроде:
Testing started at 17:34 ...
> Task :cleanTest
> Task :compileJava NO-SOURCE
> Task :compileGroovy UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :compileTestGroovy UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass$3$1 (file:/C:/Users/jmoreno/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.3.11/f6b34997d04c1538ce451d3955298f46fdb4dbd4/groovy-all-2.3.11.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass$3$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
com.example.project.CalculatorTestsJava > addsTwoNumbers() PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[1] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[2] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[3] PASSED
com.example.project.CalculatorTestsJava > add(int, int, int)[4] PASSED
com.example.project.CalculatorTests > add(int, int, int)[1] PASSED
com.example.project.CalculatorTests > add(int, int, int)[2] PASSED
com.example.project.CalculatorTests > add(int, int, int)[3] PASSED
com.example.project.CalculatorTests > add(int, int, int)[4] PASSED
com.example.project.CalculatorTests > 1 + 1 = 2() PASSED
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 3s
5 actionable tasks: 2 executed, 3 up-to-date
17:34:08: Tasks execution finished ':cleanTest :test --tests *'.
Еще один момент, который необходимо учитывать, - это расположение классов. Согласно документации, когда мы используем плагин groovy , мы можем смешивать файлы java & groovy только в папке groovy, в этом случае класс groovy находится в java папка, в которую мы можем поместить только java файлов.
Важно также использовать правильные зависимости, если вы объявляете разные версии groovy, это может привести к ошибкам компиляции.