'What are the necessary imports for unit testing with Mockito?
I'm using Mockito version 1.9.0. I've looked around and have found that most examples ignore the list of imports, and other examples are very inconsistent with one another. I'm getting "error: cannot find symbol" for some of the annotations. I might be mixing junit syntax in with Mockito syntax, but I'm not sure.
@RunWith(MockitoJUnitRunner.class)
public class SomeControllerTest {
@Mock
private SomeServiceImpl someService;
@InjectMocks
private SomeController someController;
@Before
public void setUp() throws Exception {
...
}
@Test
public void testSomething() throws SomeException {
...
}
}
Edit
Here's what I'm currently importing:
import org.mockito.*;
import org.mockito.runners.MockitoJUnitRunner;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
Solution 1:[1]
usually you are fine with :
import static org.mockito.Mockito.*;
With today's IDE, they provide autocompletion, and they will lookup upper classes. For example Mockito class is designed in this way, if you try to use the Mockito eq matcher, IntelliJ will import org.mockito.Matchers.eq
In the same way you could use the given family methods ti use BDD style when stubbing instead of when family methods, it will import org.mockito.BDDMockito which extends the Mockito class.
Annotation are all here in org.mockito
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
For some reason regarding code organisation, other stuff might be found elsewhere like :
import org.mockito.runners.MockitoJUnitRunner;
EDIT : In Mockito 2.x, org.mockito.runners.MockitoJUnitRunner is deprecated, JUnit related code has been moved to org.mockito.junit, as such the import should be replaced by :
import org.mockito.junit.MockitoJUnitRunner;
Same for the new MockitoRule.
import org.mockito.junit.MockitoRule;
import org.mockito.junit.MockitoJunit;
But it shouldn't be an issue to find it.
Hope that helps!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
