java.lang.NoSuchMethodError
错误通常是由于类路径中存在多个版本的库,或者库的版本不兼容导致的
pom.xml
文件中:<properties>
<powermock.version>2.0.9</powermock.version>
<mockito.version>3.11.2</mockito.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</基类>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
pom.xml
文件中,可以通过以下方式排除传递依赖:<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
build.gradle
文件中排除传递依赖:dependencies {
testImplementation("org.powermock:powermock-module-junit4:${powermockVersion}") {
exclude group: 'org.mockito', module: 'mockito-core'
}
testImplementation "org.powermock:powermock-api-mockia2:${powermockVersion}"
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
}
领取专属 10元无门槛券
手把手带您无忧上云