Reputation: 2036
Hi guys I'm having trouble creating a mock. Here is the code:
The class that I am trying to mock:
public class MyClass extends BaseClass<ClassView>{
//code goes here
}
On the test:
MyClass mockMyClass;
@Test
public void setUp(){
mockMyClass = mock(MyClass.class);
}
Also tried:
@Mock MyClass mockMyClass;
@Test
public void setUp(){
MockitoAnnotations.initMocks(this)
}
The error:
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.packageName.MyClass
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
I'm thinking there is an issue when creating a mock for a class that extends a class with generic parameters. Can someone point me in the right direction?
Upvotes: 0
Views: 795
Reputation: 47945
You forgot two mandatory annotations of your TestClass:
@RunWith(PowerMockRunner.class)
@PrepareForTest({MyClass.class})
public class FirstTest {
MyClass mockMyClass;
@Test
public void setUp(){
mockMyClass = mock(MyClass.class);
}
}
Your test needs to be executed with the PowerMockRunner
and you also need to prepare for test all classes you want to mock. If you keep that in mind it will work fine :)
Upvotes: 4