The Type Mockitoannotations. Mock Is Deprecated

I am mocking a object using @Mock annotation

@Mock
Customer customer;

but the @Mock annotation is showing warning

The type MockitoAnnotations.Mock is deprecated

and my test case is failed

4 Answers

use

MockitoAnnotations.openMocks(this);

You are using the wrong mock. Try using the below one

org.mockito.Mock instead of org.mockito.MockitoAnnotations.Mock

2

You may consider removing the deprecated

@BeforeEach
void init() {
    MockitoAnnotations.initMocks(this);
}

and replace it with this rule inside the class

@Rule //initMocks
public MockitoRule rule = MockitoJUnit.rule();    

Source

1

I know this is late, but for those who run into this issue today you probably included the wrong dependency. Make sure to add:

testImplementation 'org.powermock:powermock-api-mockito2:2.0.2'

where it's mockito2.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest