How to fix 'compileTestJava error: cannot find symbol' by using gradle

I want to start some test cases by using gradle.
As soon as I start the following command via terminal:
gradle clean chromeTest -Durl="https://www.hh.de"

I’m getting the following error output at the :compileTestJava step immediately :

/home/jawar/git/seleniumHH/src/test/java/content/Tina_Tickets.java:9: 
error: cannot find symbol
import org.testng.Assert;
                 ^
symbol:   class Assert
location: package org.testng.annotations

/home/jawar/git/seleniumHH/src/test/java/content/Tina_Tickets.java:10: 
error: cannot find symbol
import org.testng.annotations.AfterTest;
                             ^
symbol:   class AfterTest
location: package org.testng.annotations

/home/jawar/git/seleniumHH/src/test/java/content/Tina_Tickets.java:11: 
error: cannot find symbol
import org.testng.annotations.BeforeTest;
                             ^
symbol:   class BeforeTest
location: package org.testng.annotations

/home/jawar/git/seleniumHH/src/test/java/content/Tina_Tickets.java:12: 
error: cannot find symbol
import org.testng.annotations.Parameters;
                             ^
symbol:   class Parameters
location: package org.testng.annotations

/home/jawar/git/seleniumHH/src/test/java/content/Tina_Tickets.java:13: 
error: cannot find symbol
import org.testng.annotations.Test;
                             ^
symbol:   class Test
location: package org.testng.annotations

My test classes are saved in path: src/test/java
This is my build.gradle script:

apply plugin: 'java'
apply plugin: 'eclipse'


jar {
    version  '1.0'
    baseName 'SeleniumStarter'
    extension '.jar'
}

description = "Gradle Selenium WebDriver"
sourceCompatibility = 1.8
targetCompatibility = 1.8

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}



repositories {

    mavenCentral()
    mavenLocal() 
}

ext.seleniumVersion = '3.12.0'

dependencies {
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-server', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-edge-driver', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-firefox-driver', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-api', version:seleniumVersion
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version:seleniumVersion

    compile group: 'org.uncommons', name: 'reportng', version:'1.1.4'
    testCompile group: 'junit', name: 'junit', version:'4.12'
    testCompile group: 'org.testng', name: 'testng', version:'6.11'
}


task chromeTest (type: Test) {
    useTestNG() {
    suites 'src/test/resources/hhde.xml'
    }
    systemProperties(System.getProperties())
        println System.properties['url']
        testLogging.showStandardStreams = true
}

eclipse {
  classpath {
  containers 'org.springsource.ide.eclipse.gradle.classpathcontainer'}
}

// A custom task to show report on tests that have run
task viewResults(dependsOn: ['seleniumTest','contentTest'] , type:Exec) {
        workingDir './build/reports/tests'      
        commandLine 'cmd', '/c', 'start index.html' 
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.10' //we want gradle 2.10 to run this project
}

task logInfo (dependsOn: ['seleniumTest','contentTest']){
    logging.captureStandardOutput LogLevel.INFO
    doLast {

        println 'test'
        println System.properties['url']
        println 'url'

    }

task printProps {
    doLast {
        println commandLineProjectProp
        println gradlePropertiesProp
        println systemProjectProp
        println envProjectProp
        println System.properties['system']

        }
    }
}

I assume it has something to do with my classpath but I couldn’t figure out what it is specifically.
Really glad for any help.

Hi,

The issue is related to that dependency: compile group: 'org.uncommons', name: 'reportng', version:'1.1.4'

You can see that it induces a conflict with testng 6.11:

$ ./gradlew dependencies --configuration=testCompile
[...]
+--- org.uncommons:reportng:1.1.4
|    +--- org.testng:testng:[5.0,) -> 7.0.0-beta4
|    |    \--- com.beust:jcommander:1.72
|    \--- velocity:velocity:1.4
|         \--- velocity:velocity-dep:1.4
\--- org.testng:testng:6.11 -> 7.0.0-beta4 (*)

So, either you fallback on a previous version of reportng in line with your testng implementation. Or, should you cannot or do not want to do that, you can try to prevent testng 7+ to enter the testCompile classpath with:

configurations.testCompile {
    resolutionStrategy {
        force 'org.testng:testng:6.11'
    }
}

Note that testng 7+ still enters the testRuntime classpath with that solution.

On a side note, if I may, you should consider to upgrade your gradle wrapper version. It would net you several benefits:

  • more options to handle conflicts
  • more configurations to manage dependencies (compileOnly, …)
  • and it would be easier to find help I think, given that the version 2.10 starts to be pretty old by Gradle standards

Should you need to validate that your code integrates well with a specific Gradle version (2.10 for example) you may use the Gradle TestKit to run integration tests against several versions of Gradle.

1 Like

It worked. Thank you for your great help. I appreciate it!

Now I’m facing a new issue that my test class is failing.

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':seleniumTest'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
        at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:203)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:185)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:66)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:50)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:25)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:110)
        at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
        at org.gradle.execution.DefaultBuildExecuter.access$000(DefaultBuildExecuter.java:23)
        at org.gradle.execution.DefaultBuildExecuter$1.proceed(DefaultBuildExecuter.java:43)
        at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30)
        at org.gradle.initialization.DefaultGradleLauncher$4.run(DefaultGradleLauncher.java:154)
        at org.gradle.internal.Factories$1.create(Factories.java:22)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:52)
        at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:151)
        at org.gradle.initialization.DefaultGradleLauncher.access$200(DefaultGradleLauncher.java:32)
        at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:99)
        at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:93)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:62)
        at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:93)
        at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:82)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:94)
        at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
        at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:43)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:78)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:48)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:51)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:28)
        at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:43)
        at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:170)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
        at org.gradle.launcher.Main.doAction(Main.java:33)
        at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
        at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
Caused by: org.gradle.api.GradleException: There were failing tests. See the report at: file:///home/jawar/git/seleniumHH/build/reports/tests/index.html
        at org.gradle.api.tasks.testing.Test.handleTestFailures(Test.java:1157)
        at org.gradle.api.tasks.testing.Test.executeTests(Test.java:570)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:227)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:220)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:209)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:585)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:568)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)

I chose that solution you gave me by forcing the test compiler to stay on testng 6.11.

These are my simple test files just for testing purposes.

package Testng;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class NewTest {
  @Test
  public void f() {
  }
  @BeforeTest
  public void beforeTest() {
  }

  @AfterTest
  public void afterTest() {
  }

}
    <?xml version="1.0" encoding="UTF-8"?>
     <suite name="Suite" parallel="false">
       <test name="Test">
         <classes>
           <class name="Testng.NewTest"/>
         </classes>
     </test> <!-- Test -->
     </suite> <!-- Suite -->

Couldn’t figure out what the problem is so far.
Do you have an idea?

testng 7+ is the culprit again. As I said earlier:

Note that testng 7+ still enters the testRuntime classpath with that solution.

Exclude it from test runtime classpath as well:

configurations.testRuntime {
    resolutionStrategy {
        force 'org.testng:testng:6.11'
    }
}

Normally the test result should fail with only that since your hhde.xml is not valid:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite" parallel="false">
  <test name="Test">
    <classes>
      <class name="org.example.FooTest"/> <!-- java packages should not contain uppercase letters -->
    </classes>
  </test>
</suite>