1

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:

  1. Added bytebuddy as an explicit dependency as per https://stackoverflow.com/a/58200255/3188761

  2. 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

  3. 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

  4. 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

  5. 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
}
halfer
  • 19,824
  • 17
  • 99
  • 186
90abyss
  • 7,037
  • 19
  • 63
  • 94
  • Please [edit] the post and show the (relevant) code. – Turing85 Dec 01 '22 at 21:44
  • 1
    What is your question? I think Mockito explained clearly what the issue is "cannot mock this class: class com.x.ServiceClient" as "Mockito can only mock non-private & non-final classes." – Adam Siemion Dec 01 '22 at 21:46
  • @AdamSiemion ServiceClient is neither private not final – 90abyss Dec 01 '22 at 21:54
  • Only partially. The code actually creating the mock is missing. --- Is this a bazel-based project? – Turing85 Dec 01 '22 at 22:07
  • one more thing to add: this build works on gradle. but not on bazel. so something to do with dependency i am guessing. – 90abyss Dec 01 '22 at 22:11
  • With JUnit5, we normally use `@ExtendWith(MockitoExtension.class)` on the test-class instead of `MockitoAnnotations.initMocks(this)` in a setup method. Despite this, the relevant code is STILL missing, i.e. the definition of the test class, and the mocks therein. – Turing85 Dec 01 '22 at 22:15
  • @90abyss Try removing mockito-core. mockito-inline is an replacement for it - you only need one. – Lesiak Dec 02 '22 at 13:30

0 Answers0