Generate static imports in Eclipse on autocomplete for JUnit Assert, StringUtils and others

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.

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:

  1. Go to Window > Preferences > Java > Editor > Content Assist.
  2. 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:

  1. Go to Window > Preferences > Java > Editor > Content Assist > Favorites.
  2. Click New Type. This brings up a dialog where you can enter the fully qualified type.
  3. 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.*.
  4. 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:

Share this tip

Support Eclipse On E

10 thoughts on “Generate static imports in Eclipse on autocomplete for JUnit Assert, StringUtils and others

    • 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.

  1. 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.

  2. Thanks a lot! I was thinking about stopping using the new JUnit and Hamcrest matchers because of that issue.

  3. 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