Using org.gradle.api.tasks.testing.Test.beforeTest in java


How can I rewrite the following groovy code into java:

            Test test = ....
            test.beforeTest { descriptor ->
                println("Starting " + descriptor.toString())

due to beforeTest method only can recieve an Closure, not an Action…

Thanks for help

The beforeTest(Closure) method is just a shortcut you can use in Groovy. Ultimately, it maps the closure to receive broadcasts the same as the TestListener interface. You can clean up how you handle this vs. using the anonymous class, but essentially:

test.addTestListener(new TestListener() {
    void beforeSuite(TestDescriptor suite) {}
    void afterSuite(TestDescriptor suite, TestResult result) {}
    void beforeTest(TestDescriptor testDescriptor) {
        println("Starting " + testDescriptor.toString());
    void afterTest(TestDescriptor testDescriptor, TestResult result) {}

Thank you, it works :slight_smile: