I have gone through multiple Stack Overflow posts but none of the suggested answers have thus far helped me.
Details:
Java 11
Mockito core 4.3.1
Byte Buddy 1.12.7
Spring beans 5.3.20
Sprint context 5.3.20
Error:
test1_standardSubgraph() (com.x.HelperTest)
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.x.ServiceClient.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
What I have tried so far:
Added bytebuddy as an explicit dependency as per https://stackoverflow.com/a/58200255/3188761
Ensured that no other dependency is bringing in another version of byte-buddy transitively except for mockito-core as per https://stackoverflow.com/a/62002689/3188761
Made sure the version of byte-buddy matches the one linked here https://github.com/mockito/mockito/blob/v4.3.1/gradle/dependencies.gradle#L7. Checked as per https://stackoverflow.com/a/63640379/3188761
Made sure that mockito is not brought in transitively by another dep like spring-boot-starter-test. I am not even using spring-boot-starter-test in my dependency. Checked as per https://stackoverflow.com/a/60210414/3188761
Tried downgrading to lower major version but still no luck.
Code:
ServiceClient.java:
public class ServiceClient {
@Autowired
public ServiceClient(ClientFactory factory) {
...
}
}
WORKSPACE:
JUNIT_JUPITER_VERSION = "5.8.2"
JUNIT_PLATFORM_VERSION = "1.9.1"
JACKSON_CORE_VERSION = "2.14.1"
GUAVA_VERSION = "31.1-jre"
APACHE_COMMONS_LANG3_VERSION = "3.12.0"
APACHE_COMMONS_LANG_VERSION = "2.6"
JETBRAINS_ANNOTATIONS_VERSION = "23.0.0"
LOMBOK_VERSION = "1.18.24"
SPRING_VERSION = "5.3.20"
KOTLIN_REFLECT_VERSION = "1.7.21"
FINDBUGS_VERSION = "3.0.2"
MOCKITO_VERSION = "3.2.0"
maven_install(
artifacts = [
"com.fasterxml.jackson.core:jackson-databind:%s" % JACKSON_CORE_VERSION,
"com.google.guava:guava:%s" % GUAVA_VERSION,
"org.apache.commons:commons-lang3:%s" % APACHE_COMMONS_LANG3_VERSION,
"commons-lang:commons-lang:%s" % APACHE_COMMONS_LANG_VERSION,
"org.jetbrains:annotations:%s" % JETBRAINS_ANNOTATIONS_VERSION,
"org.projectlombok:lombok:%s" % LOMBOK_VERSION,
"org.springframework:spring-beans:%s" % SPRING_VERSION,
"org.springframework:spring-context:%s" % SPRING_VERSION,
"org.jetbrains.kotlin:kotlin-reflect:%s" % KOTLIN_REFLECT_VERSION,
"org.junit.platform:junit-platform-launcher:%s" % JUNIT_PLATFORM_VERSION,
"org.junit.platform:junit-platform-reporting:%s" % JUNIT_PLATFORM_VERSION,
"org.junit.jupiter:junit-jupiter-api:%s" % JUNIT_JUPITER_VERSION,
"org.junit.jupiter:junit-jupiter-params:%s" % JUNIT_JUPITER_VERSION,
"org.junit.jupiter:junit-jupiter-engine:%s" % JUNIT_JUPITER_VERSION,
"com.google.code.findbugs:jsr305:%s" % FINDBUGS_VERSION,
"org.mockito:mockito-core:%s" % MOCKITO_VERSION,
"org.mockito:mockito-inline:%s" % MOCKITO_VERSION,
"org.mockito:mockito-junit-jupiter:%s" % MOCKITO_VERSION,
],
repositories = [
"https://repo1.maven.org/maven2",
],
)
BUILD:
java_test_suite(
name = "lib-tests",
srcs = glob(["src/test/java/**/*.java"]),
runner = "junit5",
test_suffixes = ["Test.java"],
runtime_deps = JUNIT5_DEPS,
deps = [
"//:lib",
"@maven//:org_jetbrains_annotations",
"@maven//:org_springframework_spring_beans",
"@maven//:org_springframework_spring_context",
artifact("org.jetbrains.kotlin:kotlin-reflect"),
artifact("org.junit.jupiter:junit-jupiter-api"),
artifact("org.junit.jupiter:junit-jupiter-params"),
artifact("org.mockito:mockito-core"),
artifact("org.mockito:mockito-inline"),
artifact("org.mockito:mockito-junit-jupiter"),
],
)
Test failing here:
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this); // HERE
}