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 .
Views
Replies
Total Likes
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
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 .
Views
Replies
Total Likes
@newbie34 Just checking in — were you able to resolve your issue?
We’d love to hear how things worked out. If the suggestions above helped, marking a response as correct can guide others with similar questions. And if you found another solution, feel free to share it — your insights could really benefit the community. Thanks again for being part of the conversation!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Like
Replies
Views
Likes
Replies
Views
Like
Replies