Finding Static Import Statements for Mockito Structures

I'm trying to break through a brick wall between me and Mokito. I tore my hair while trying to get the right import static statements for Mockito material. You might think that someone just throws out a table saying that anyInt () comes from org.mockito.Matchers and when () comes from org.mockito.Mockito, etc., but it will be too useful for beginners , no?

Things like this, especially when mixed with many import statements that end with asterisks, are not always very useful:

import static org.junit.Assert.*; import static org.mockito.Mockito.*; 

Yes, I know and am trying to use the window Eclipse Window -> Preferences-> Java -> Editor-> Content Assist -> Favorites. It helps, but it does not apply to the nail on the head.

Any answers to this question will be appreciated.

Thanks a lot, Russ

+50
java mockito static-import
Sep 06 2018-11-11T00:
source share
3 answers

The problem is that static imports from Hamcrest and Mockito have similar names, but return Matches and real values ​​respectively.

One workflow is simply to copy the Hamcrest and / or Mockito classes and remove / rename static functions so that they are easier to remember and less displayed in autocompletion. This is what I did.

Also, when using mocks, I try to avoid assertThat in favor of other other assertions and verify , for example.

 assertEquals(1, 1); verify(someMock).someMethod(eq(1)); 

instead

 assertThat(1, equalTo(1)); verify(someMock).someMethod(eq(1)); 

If you remove classes from favorites in Eclipse and enter a long name, for example. org.hamcrest.Matchers.equalTo and do CTRL + SHIFT + M in "Add Import", then autocomplete will show you only Hamcrest matches, not Mockito. And you can do it differently until you mix matches.

+15
06 Sep 2018-11-11T00:
source share
— -

Here is what I did to deal with the situation.

I am using global imports in a new test class.

 import static org.junit.Assert.*; import static org.mockito.Mockito.*; import static org.mockito.Matchers.*; 

When you finish writing your test and you need to make a deal, you simply CTRL + SHIFT + O to organize the packages. For example, you can just leave:

 import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.Matchers.anyString; 

This allows you to encode the code without the need to get stuck trying to find the right package to import.

+83
Sep 13 2018-11-11T00:
source share

For is ()

 import static org.hamcrest.CoreMatchers.*; 

For assertThat ()

 import static org.junit.Assert.*; 

In the case when () and verify ()

 import static org.mockito.Mockito.*; 
0
Jul 23 '15 at 7:44
source share



All Articles