22

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

Mosam Mehta
  • 1,658
  • 6
  • 25
  • 34
Girdhar Singh Rathore
  • 5,030
  • 7
  • 49
  • 67

4 Answers4

32

use

MockitoAnnotations.openMocks(this);
Huang Jinlong
  • 774
  • 8
  • 6
6

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

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

Alexander
  • 2,925
  • 3
  • 33
  • 36
robin
  • 1,893
  • 1
  • 18
  • 38
3

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

Pipo
  • 4,653
  • 38
  • 47
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.

6rchid
  • 1,074
  • 14
  • 19