8

I have roughly the following setup:

test-utils/build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE'
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

dependencies {
    compile ('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.mockito'
        exclude group: 'org.hamcrest'
    }
    compile  'org.mockito:mockito-core:2.0.41-beta'
    compile  'org.assertj:assertj-core:3.3.0'
}

main/build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.3.1.RELEASE'
    }
}

apply plugin: 'java'
apply plugin: 'spring-boot'

dependencies {
    testCompile project(':test-utils')
}

But for some reason it seems that the spring boot plugin forces the mockito version back to 1.x:

# ./gradlew :main:dependencies --configuration=testCompile

testCompile - Compile classpath for source set 'test'.
+--- project :test-utils
     +--- org.springframework.boot:spring-boot-starter-test: -> 1.3.1.RELEASE
     |    +--- junit:junit:4.12
     |    +--- org.springframework:spring-core:4.2.4.RELEASE
     |    \--- org.springframework:spring-test:4.2.4.RELEASE
     |         \--- org.springframework:spring-core:4.2.4.RELEASE
     +--- org.mockito:mockito-core:2.0.41-beta -> 1.10.19
     |    +--- org.hamcrest:hamcrest-core:1.1 -> 1.3
     |    \--- org.objenesis:objenesis:2.1
     \--- org.assertj:assertj-core:3.3.0

If I take the spring boot plugin out of the equation, things work as expected:

# ./gradlew :main:dependencies --configuration=testCompile

testCompile - Compile classpath for source set 'test'.
+--- project :test-utils
     +--- org.springframework:spring-core:4.2.4.RELEASE (*)
     +--- org.springframework:spring-test:4.2.4.RELEASE
     |    \--- org.springframework:spring-core:4.2.4.RELEASE (*)
     +--- junit:junit:4.12
     +--- org.mockito:mockito-core:2.0.41-beta
     |    +--- net.bytebuddy:byte-buddy:1.0.2
     |    \--- org.objenesis:objenesis:2.1
     \--- org.assertj:assertj-core:3.3.0

What is the spring boot plugin doing exactly, and how can I tell it not to?

Amir Abiri
  • 8,847
  • 11
  • 41
  • 57

2 Answers2

8

Your main project has Spring Boot's plugin applied to it, so it's using Spring Boot's dependency management. That means it will, by default, use Spring Boot's preferred version of Mockito, irrespective of the version specificied in test-utils.

As described in the documentation, you can override the version of a dependency that Spring Boot manages by setting the relevant property. For Mockito that property is mockito.version. Add the following to your main project:

ext['mockito.version'] = '2.0.41-beta'
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 1
    I just knew the answer would include a "as described in the documentation" hit... :-/. I searched a lot with "spring boot gradle" but now that I know the answer is under "Build", I notice that [this](https://github.com/spring-gradle-plugins/dependency-management-plugin/#overriding-versions-in-a-bom) is one click away from [here](https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html). Sigh. – Amir Abiri Jan 28 '16 at 09:18
0

I think you can use configuration setting to force the version

    allprojects {

        configurations.all {
            resolutionStrategy { rs ->
                rs.force "org.mockito:mockito-core:2.0.41-beta"
            }
        }
    }
sendon1982
  • 9,982
  • 61
  • 44