Expand my Community achievements bar.

Introducing Adobe LLM Optimizer: Own your brand’s presence in AI-Powered search and discovery

[Junit 5 ] Failed to release mocks error

Avatar

Level 3

Hi guys ,

While building code with Oracle jdk-21.0.5 , I am getting below error for test class :-

This should not happen unless you are using a third-party mock maker
[ERROR] <MockitoAnnotations.initMocks(this);> Mockito
Failed to release mocks

 

Dependencies used :-

 

<dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.11.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
<dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>5.16.0</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-junit-jupiter</artifactId>
                <version>5.16.0</version>
                <scope>test</scope>
            </dependency>
    		<dependency>
			    <groupId>org.mockito</groupId>
			    <artifactId>mockito-inline</artifactId>
			    <version>5.2.0</version>
			    <scope>test</scope>
			</dependency>
            <dependency>
                <groupId>junit-addons</groupId>
                <artifactId>junit-addons</artifactId>
                <version>1.4</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>io.wcm</groupId>
                <artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
                <version>5.1.2</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.sling</groupId>
                <artifactId>org.apache.sling.testing.caconfig-mock-plugin</artifactId>
                <version>1.4.0</version>
            </dependency>

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.5.2</version>
                    <configuration>
                        <useSystemClassLoader>false</useSystemClassLoader>
                    </configuration>
                </plugin>

 

If anyone can help with the solution .

2 Replies

Avatar

Community Advisor

Hi, 

You may try Try adding @ExtendWith(MockitoExtension.class) on top of the test class and removing MockitoAnnotations.initMocks(this); from setUp method

Since you're using JUnit 5, you should prefer:

@ExtendWith(MockitoExtension.class) class MyTest { @Mock MyService service; }

instead of:

@BeforeEach void setUp() { MockitoAnnotations.initMocks(this); // OLD STYLE }

OR try upgrading  version of mockito-inline

Thanks

Avatar

Level 3

Hi @MukeshYadav_ ,thank you  for replying ,

After using @ExtendWith(MockitoExtension.class) , 

getting below error for globally mocked  variables (eg -

 
 @Mock
 ValueMap valMap;

) -- 

 

Mockito couldn't inject mock dependency on field 'org.apache.sling.api.resource.ValueMap com.adobe.cq.sightly.WCMUsePojo.inheritedPageProperties' that is annotated with @InjectMocks in your test,
because there were multiple matching mocks (i.e. fields annotated with @Mock and having matching type)
If you have multiple fields of same type in your class under test then consider naming the @Mock fields identically to the respective class under test's fields, so Mockito can match them by name.

 

If I mock same variables locally  ( eg:- ValueMap valMap = Mockito.mock(ValueMap.class); )-

then null is assigned to them .

 

I am using mockito-inline v5.2.0 , which is the latest .

Please let me know how can I solve this issue .