Gradle can't find my TestNgListener

Hello everyone.

Having a problem with getting listener for my TestNg test in my project.

MyListener extends org.testng.IReporter, and is located in project’s buildSrc class.

but when I am trying to use it in build.gradle:

  test {

useTestNG()

options {

listeners.add(“com.listener.MyListener”)

} }

so when I am executing

 > gradle test --debug
I am getting an error log:
  …  [TestEventLogger] Gradle Test Executor 1 STARTED  …  [TestEventLogger] Gradle Test Executor 1 FAILED  [TestEventLogger]

org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for process ‘Gradle Test Executor 1’. …

Caused by: [TestEventLogger]

org.gradle.api.GradleException: Could not add a test listener with class ‘com.listener.MyListener’. … Caused by: [TestEventLogger]

java.lang.ClassNotFoundException: com.listener.MyListener … [TestEventLogger] Test Run FAILED

What can be the problem?

Also may add that if I am adding to doLast() tag to my build.gradle:

  test << {

useTestNG()

options {

listeners.add(“com.listener.MyListener”)

} }

error is not causing, but Listener is not executing anyway. Gradle is even not starting to launch MyListener…

Maybe it’s just I need to set up some more options?

Your listener should be on test classpath and not on build script classptath. You should use the snippet from your original post to register the listener and move the listener class to test source set.

Can you explain a little more. I don’t understand second sentance at all. And correct me if I understood your first sentance: compiled classes from buildSrc folder are including into buildScript classpath of my build.gradle, but not on test classpath.

Yes, you understood the first sentence correctly. The second sentence means that you need to move your listener class from buildSrc to src/test/java. That’s where your test source set would live if your project follows the convention.

Thanks for explanation. Will try it a little later, and report about my results

Yeah, so the solution is to move listener in to the package with my tests!

But still there is a little trouble, I am having a multi-project, so it was usefull for me to declare it once somewhere, but not duplicating the code everywhere. Now because of this I have three choises: 1) Duplicate Listener codes for all subprojects 2) Make a project with the listener and make a dependency for all subprojects, to compile Listener-project 3) Put it to some existed project, and make dependencies for other subprojects.

Actually no one of those solutions I don’t like.

Yeah that helped, It started to execute my listener, but there is a little trouble… written in the reply below

The best solution would be probably to create a new subproject that will only contain that listener and then add the following dependency to all subprojects that need the listener:

dependencies {
    testCompile project(":test-listener-project")
}

If all of your subprojects apart from the listener one need it then you can use the following in your root project build script:

subprojects {
    if (name != "test-listener-project") {
        dependencies {
            testCompile project(":test-listener-project")
        }
    }
}

Yes, that seems the best solution…

By the way is there a set of options that needs to be specified for Listeners? I am specifing only ‘outputDirectory’ - as a path to folder with results. Well and listeners ofcourse.

I was thinking that gradle will take as default all testCases, but seems it’s not.

Will try to handle this for my own. But would be good if somebody will ahead me, and explain which are defenetly should be specified