43

I'm new to gradle and I'm getting a build error that I don't really understand. My project is just an empty shell with the directory structure and no java source code. Here is my root build.gradle file

allprojects {
    //Put instructions for all projects
    task hello << { task -> println "I'm $task.project.name" }
}

subprojects {
    //Put instructions for each sub project
    apply plugin: "java"
    repositories {
        mavenCentral()
    }
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

when I execute the gradle build command the build fails because it doesn't know the testCompile method with this message:

Could not find method testCompile() for arguments [{group=junit, name=junit, version=4.+}] on root project

I use Gradle 2.5.

I've understood that this method is a part of the java plugin which I've loaded. I don't see what went wrong, can you help?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Jib'z
  • 953
  • 1
  • 12
  • 23
  • that dependencies block probably belongs inside the subprojects block, yeah? – dnault Jul 14 '16 at 23:21
  • It's complaining that testCompile doesn't exist because you haven't applied the 'java' plugin to *this* build file. – dnault Jul 14 '16 at 23:22
  • As I already said, i'm new to gradle, so how can i apply the java plugin to this build file ? – Jib'z Jul 14 '16 at 23:30
  • Unless the root project has java source code, it doesn't make sense to apply the java plugin... and it doesn't make sense to declare dependencies for the root project. If your intent is to say that each of the subprojects depends on JUnit, then that dependencies block belongs *inside* the subprojects block. – dnault Jul 14 '16 at 23:34

4 Answers4

89

In case anyone comes here based on the Could not find method testCompile() error, by now the more probable cause is that you need to replace the deprecated testCompile by testImplementation. See What's the difference between implementation and compile in Gradle?

PHPirate
  • 7,023
  • 7
  • 48
  • 84
  • As a corollary : another possibility is that you are using a different Gradle version than your project was set up with. Check whether or not there is a Gradle Wrapper (`gradlew`) in the project. For example, `gradle dependencies` might become `./gradlew dependencies` (from the root) and thus you would use the version configured by the project owner. – payne Mar 03 '22 at 18:59
13

The java plugin is only applied to subprojects, so the testCompile configuration, added by the java plugin, can only be used in subprojects. The below works:

allprojects {
    //Put instructions for all projects
    task hello << { task -> println "I'm $task.project.name" }
}

subprojects {
    //Put instructions for each sub project
    apply plugin: "java"
    repositories {
        mavenCentral()
    }



    dependencies {
        testCompile group: 'junit', name: 'junit', version: '4.+'
    }
}
Nicolas Modrzyk
  • 13,961
  • 2
  • 36
  • 40
0

Please check Gradle version used in the project.

compile and testCompile configurations are removed in Gradle 7+. You can use implementation and testImplementation instead.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 15 '23 at 12:10
-1

It's saying that it can't find the method testCompile for the arguments check that you spelt it correctly making sure the name and group which you have as "junit" are all correct and also the version is correct another fix for this issue is adding the testCompile line in sub projects block.

RyanM
  • 751
  • 5
  • 20