Eclipse normally adds import statements for you when you autocomplete a type, press Ctrl+Shift+O or organise imports when you save. However, by default it doesn’t do the same for static imports, something you’ll miss when using classes like JUnit’s Assert and Apache Commons’ StringUtils. This means having to add import statements manually, which takes unnecessary time.
So, in the code below, I want to be able to autocomplete assertEquals (line 7) and have Eclipse add a static import for org.junit.Assert.assertEquals (line 1).
import static org.junit.Assert.assertEquals; ... public class MyTest { @Test public void testSomething() throws Exception { assertEquals(1, 1); }
Now, the good news is that there is a way to automatically add static imports when you use autocomplete. It’s done via a preference that tells Eclipse which static classes you want to include for Content Assist (autocomplete). I show you how to do this below.
The bad news is that there doesn’t seem to be a way to tell the Organise Imports command to add static imports. However, a working autocomplete should already cater for 90% of your coding needs, so the bad news isn’t that bad.
Advertisement
How to add static imports on autocomplete
For this example, we’ll set up JUnit’s Assert class so that Eclipse will automatically add org.junit.Assert.assertEquals to the imports if we autocomplete any method on Assert, such as assertEquals.
There are two preferences that work together to enable this feature. The first one just needs verification:
- Go to Window > Preferences > Java > Editor > Content Assist.
- Make sure the option Use static imports (only 1.5 or higher) is enabled (it should be enabled by default, but it’s good to verify this anyway).
Here’s an example of how it should look:
Next you have to tell Eclipse which classes you’d like to add as static imports. Here’s how to do this:
- Go to Window > Preferences > Java > Editor > Content Assist > Favorites.
- Click New Type. This brings up a dialog where you can enter the fully qualified type.
- Enter org.junit.Assert and click Ok (you could also use the Browse… if you want to search for the type). Eclipse will add an entry to the list which reads org.junit.Assert.*.
- Click Ok on the preferences dialog to save and close the preferences.
Here’s what my list currently looks like:
You can add an entry for any other static import you’d like, even for a static Utils class you’ve written yourself.
Now, to test that it works you can add a JUnit test, type assertEquals, press Ctrl+Space and press Enter on the appropriate entry. Eclipse will now add the static import org.junit.Assert.assertEquals (or org.junit.Assert.*, depending on your Organize Imports preferences).
As I mentioned before, this only works for autocomplete and not for organise imports commands (eg. Ctrl+Shift+O), but it is already a lot better than having to enter the import yourself and should do the job most of the time.
Related Tips
Now that you’ve got static imports to work, the following tips might be of interest:
- JUnit testing made easier with Eclipse templates
- Automatically format and cleanup code every time you save
- Run/debug a class in Eclipse as a JUnit test or Java app with a single keystroke
- Skip over certain classes when using Step Into in Eclipse’s debugger
Great tip! I’m always struggling with static imports in eclipse.
Glad I could help. I hope the static import support is improved in Eclipse in the next version or two. More and more frameworks (esp. testing, mocking and utils) are providing static methods as part of their API.
Great post, thank you !
It’s a pleasure and thanks for the compliment.
Thanks! Another superb tip which will save me countless seconds. I usually type “assertEquals”, then use the quick fix to do a static import of assert.*, then 1 minute later save the file or organise imports which replaces the wildcard import with one for assertEquals only, then 1 minute later write “assertTrue” or something and get another red line and have to quick fix again. Argh!
It’s a pleasure, Oisín. Glad I could help.
You might also want to check out the JUnit templates I’ve posted that allow you to generate assertEquals and other statements, together with the static import statement. Because the templates do a bit more than just import the method, they generally work a bit faster. I’ve just neglected to link to them from here – a point that I’ve corrected now.
But this tip isn’t just for JUnit imports but any other static import, so it’s always good knowing.
Thanks a lot! I was thinking about stopping using the new JUnit and Hamcrest matchers because of that issue.
Also, there is a short cut that could help in adding static imports. CTL+Shift+M or use menu (Source ==> Add Import)
HTH,
Thanks,
Amit Chhajed
Thanks Amit. However, this only works for normal imports but not for static imports, even if I add it to the Favorites. Should I be doing something else?
Not really. This is pretty much standard feature, at least in Eclipse Juno. And it works for the ones not added to the Favorites.
What is your eclipse version ?
Check the below link: http://stackoverflow.com/questions/288861/eclipse-optimize-imports-to-include-static-imports