代码之家  ›  专栏  ›  技术社区  ›  tpdi

PowerMock 1.7.4和Mockito 1.10.19的梯度配置

  •  0
  • tpdi  · 技术社区  · 6 年前

    我正试图用powermock version 1.7.4和mockitoversion1.10.19运行java测试,使用gradle。

    powermock 1.7.4对两者都有传递依赖关系

    mockito mockito核心1.10.19 以及在 mockito;mockito core 2.8.9

    (见 https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito-common/1.7.4 )

    为了不引入mockito 2.8.9,而是让测试针对mockito 1.10.19编译和运行,我必须执行以下操作(因为powermock jars中存在各种可传递的依赖关系):

    testCompile ("org.mockito:mockito-core:${versions.mockito}") {
        exclude group: 'org.hamcrest', module: 'hamcrest-core'
    }
    
    testCompile ("org.powermock:powermock-api-mockito-common:${versions.powermock}") {
        exclude group: 'org.mockito', module: 'mockito-core'
    }
    
    testCompile ("org.powermock:powermock-api-mockito:${versions.powermock}") {
        exclude group: 'org.mockito', module: 'mockito-core'
    }
    
    testCompile ("org.powermock:powermock-core:${versions.powermock}") {
        exclude group: 'org.mockito', module: 'mockito-core'
    }
    
    testCompile ("org.powermock:powermock-module-junit4:${versions.powermock}") {
        exclude group: 'org.powermock', module: 'powermock-module-junit4-common'
    }
    
    testCompile ("org.powermock:powermock-module-junit4-common:${versions.powermock}") {
        exclude group: 'org.powermock', module: 'powermock-core'
    }
    
    testCompile ("org.powermock:powermock-api-support:${versions.powermock}") {
        exclude group: 'org.powermock', module: 'powermock-core'
    }
    
    testCompile ("org.powermock:powermock-api-easymock:${versions.powermock}") {
        exclude group: 'org.powermock', module: 'powermock-api-support'
    }
    
    testCompile ("org.powermock:powermock-module-javaagent:${versions.powermock}") {
        exclude group: 'org.powermock', module: 'powermock-core'
    }
    
    testCompile ("org.powermock:powermock-module-junit4-rule-agent:${versions.powermock}") {
        exclude group: 'org.powermock', module: 'powermock-core'
    }
    
    testRuntime("org.mockito:mockito-core:${versions.mockito}") {
        exclude group: 'org.hamcrest', module: 'hamcrest-core'
    }
    

    其中版本为:

    versions = [
            'mockito': '1.10.19',
            'powermock': '1.7.4'
    ]
    

    当然,在格雷德有更好,更简洁的方法来做这件事,对吧?

    1 回复  |  直到 6 年前
        1
  •  1
  •   ToYonos    6 年前

    一种压缩它的方法,不包括 mockito-core :

    testCompile ("org.mockito:mockito-core:${versions.mockito}") {
        exclude group: 'org.hamcrest', module: 'hamcrest-core'
    }
    
    ['powermock-api-mockito-common',
    'powermock-api-mockito',
    'powermock-core',
    'powermock-module-junit4',
    'powermock-module-junit4-common',
    'powermock-api-support',
    'powermock-api-easymock',
    'powermock-module-javaagent',
    'powermock-module-junit4-rule-agent'].each {
        testCompile ("org.powermock:${it}:${versions.powermock}") {
            exclude group: 'org.mockito', module: 'mockito-core'
        }  
    }
    

    此外,TESTRONTIME 莫克托核心 是无用的 testRuntime extends testCompile