mockito / mockito

Most popular Mocking framework for unit tests written in Java
http://mockito.org
MIT License
14.83k stars 2.55k forks source link

Mockito canonical name does not show mock anymore since version 5 #3395

Open jordilaforge opened 3 months ago

jordilaforge commented 3 months ago

Dear Mocktio Community

While Debugging my spring application with Intellij I realized that the name of my mockito objects does not show mock anymore. image anotherService here is a mock. Before is was shown like this: image

So I did some investigation and found out that this change was introduced with version 5.0.0 of mockito because with version 4.11.0 the canonical name still includes mock. My question is was this intentional or is this a bug? I find it very useful to see what is a mock and what is not, especially if you have a lot of dependencies.

daniel-matheis-vivavis commented 3 months ago

Just to be sure that you have taken the right screenshot in both cases: Why is the first image telling SomeService and the second SomeServiceTest Was the first image also done during testing?


And furthermore my suspicion: "Mockito 5 switches the default mockmaker to mockito-inline" https://github.com/mockito/mockito?tab=readme-ov-file#readme

This could explain that the class name is not changed anymore because the byte code of the original class is instrumented by Mockito and there is no need for another class which you have seen previously...

jordilaforge commented 3 months ago

Thank you for your response. You are right those are different breakpoints, I have updated the second screenshot at the same breakpoint like the first but its the same issue. Here is a little java maven unit test to show this:

//class which has a dependency on another class which we are going to mock
public class SomeService {

    private final AnotherService anotherService;

    public SomeService(AnotherService anotherService) {
        this.anotherService = anotherService;
    }

    public String someMethod() {
        return anotherService.getClass().getCanonicalName();
    }
}

//AnotherService has no implementation
public class AnotherService {
}

//and here my little unit test for the canonical name of the mock
    @Test
    void someMethod() {
        AnotherService mock = Mockito.mock(AnotherService.class);
        SomeService someService = new SomeService(mock);
        Assertions.assertThat(someService.someMethod()).contains("Mock");
    }
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>mockito-test-maven</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>4.11.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.10.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.assertj</groupId>
      <artifactId>assertj-core</artifactId>
      <version>3.26.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M7</version>
      </plugin>
    </plugins>
  </build>
</project>

So if I run this test with version 4.11.0 of mockito it works. But with version 5.0.0 it does not.