420

Is there anyway to get Eclipse to automatically look for static imports? For example, now that I've finally upgraded to Junit 4, I'd like to be able to write:

assertEquals(expectedValue, actualValue);

hit Ctrl + Shift + O and have Eclipse add:

import static org.junit.Assert.assertEquals;

Maybe I'm asking too much.

Jasper
  • 2,166
  • 4
  • 30
  • 50
Paul Croarkin
  • 14,496
  • 14
  • 79
  • 118
  • Does this answer your question? [Do I have to manually add static imports all the time?](https://stackoverflow.com/questions/19435598/do-i-have-to-manually-add-static-imports-all-the-time) – ddavison Jan 17 '22 at 16:45

9 Answers9

588

I'm using Eclipse Europa, which also has the Favorite preference section:

Window > Preferences > Java > Editor > Content Assist > Favorites

In mine, I have the following entries (when adding, use "New Type" and omit the .*):

org.hamcrest.Matchers.*
org.hamcrest.CoreMatchers.*
org.junit.*
org.junit.Assert.*
org.junit.Assume.*
org.junit.matchers.JUnitMatchers.*

All but the third of those are static imports. By having those as favorites, if I type "assertT" and hit Ctrl+Space, Eclipse offers up assertThat as a suggestion, and if I pick it, it will add the proper static import to the file.

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
Joey Gibson
  • 7,104
  • 1
  • 20
  • 14
  • Yeah awesome, but, Dude, you only have the matchers for hamcrest. That doesn't bring in AssertThat.. ? – Rob Apr 18 '12 at 21:05
  • [assertThat](http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertThat%28T,%20org.hamcrest.Matcher%29) is on Assert – plasma147 Jun 23 '12 at 08:53
  • 1
    If you try this in Eclipse 4.2 "Juno" it forbids appending the `.*`, so you must manually enter `New Member` -> `org.junit.Assert.assertEquals` and so on. Anyone have a better workaround for this? – KomodoDave Aug 30 '12 at 12:51
  • 28
    Ah - the trick is to simply add `org.junit.Assert` via `New Type...`, the `.*` is then implicit. – KomodoDave Aug 30 '12 at 13:01
  • 5
    Is it correct that auto-import `Ctrl+Shift+o` won't add the necessary import, and in fact you must hover the cursor and choose `Add static import..` option? It's a shame the usual auto-import selection list dialog doesn't display. – KomodoDave Aug 30 '12 at 13:07
  • This does not work anymore. I added the entries exactly as shown, but am now offered anything if I type "asserte" and and ctrl+space. Any ideas ? – J. Stoever Sep 29 '12 at 21:35
  • 2
    @J.Stoever - If you're using JUnit 4 you'll want to use the Hamcrest form: `assertThat(value, is(expected));` – David Harkness Oct 05 '12 at 00:46
  • Also use Java -> Editor -> Save Actions -> Select Organize imports . It automatically organizes your imports everytime you save. So need of using any shortcuts. – Barun Oct 24 '13 at 07:08
  • I was trying to get `assertEquals(...)`. In Eclipse kepler 4.3, I tried `asserte` followed by ctrl+space. It gets me all methods that begin with `asserte`. Thanks. – Erran Morad Oct 06 '14 at 00:02
  • You can also put the number of imports required to use `.*` in the static import to 1. But beware that in that case you shouldn't use static imports on classes that contain class members with names that are too generic (e.g. don't use static import with `.*` for classes with `getInstance`). – Maarten Bodewes Oct 16 '14 at 12:12
  • 2
    I tried this with Eclipse Kepler, but to no avail. I restarted Eclipse as well. No autocompletion, no static import choice when hovvering or pressing CTRL + 1. Anyone experienced this? – Magnilex Nov 20 '14 at 10:17
  • 2
    Intellij Idea supports this out of the box. Shame eclipse doesn't. – Neeraj Feb 22 '18 at 09:27
245

If you highlight the method Assert.assertEquals(val1, val2) and hit Ctrl + Shift + M (Add Import), it will add it as a static import, at least in Eclipse 3.4.

Jasper
  • 2,166
  • 4
  • 30
  • 50
Dave L.
  • 43,907
  • 11
  • 63
  • 62
31

Eclipse 3.4 has a Favourites section under Window->Preferences->Java->Editor->Content Assist

If you use org.junit.Assert a lot, you might find some value to adding it there.

Bill Michell
  • 8,240
  • 3
  • 28
  • 33
22

Not exactly what I wanted, but I found a workaround. In Eclipse 3.4 (Ganymede), go to

Window->Preferences->Java->Editor->Content Assist

and check the checkbox for Use static imports (only 1.5 or higher).

This will not bring in the import on an Optimize Imports, but if you do a Quick Fix (CTRL + 1) on the line it will give you the option to add the static import which is good enough.

vivekmore
  • 403
  • 5
  • 19
Paul Croarkin
  • 14,496
  • 14
  • 79
  • 118
10

From Content assist for static imports

To get content assist proposals for static members configure your list of favorite static members on the Opens the Favorites preference page Java > Editor > Content Assist > Favorites preference page.
For example, if you have added java.util.Arrays.* or org.junit.Assert.* to this list, then all static methods of this type matching the completion prefix will be added to the proposals list.

Open Window » Preferences » Java » Editor » Content Assist » Favorites

enter image description here

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
6

For SpringFramework Tests, I would recommend to add the below as well

org.springframework.test.web.servlet.request.MockMvcRequestBuilders
org.springframework.test.web.servlet.request.MockMvcResponseBuilders
org.springframework.test.web.servlet.result.MockMvcResultHandlers
org.springframework.test.web.servlet.result.MockMvcResultMatchers
org.springframework.test.web.servlet.setup.MockMvcBuilders
org.mockito.Mockito

When you add above as new Type it automatically add .* to the package.

Matthias
  • 7,432
  • 6
  • 55
  • 88
Neeraj
  • 1,163
  • 2
  • 18
  • 32
6

Shortcut for static import: CTRL + SHIFT + M

Michael Hegner
  • 5,555
  • 9
  • 38
  • 64
2

Select the constant, type

Ctrl + 1  (quick fix)

Select "Convert to static import." from the drop down.

"Quick fix" has options even though it is not an error.

teknopaul
  • 6,505
  • 2
  • 30
  • 24
1

In Eclipse 4.9, you can static import existing invocations using a quick fix.

A new quick fix has been implemented that allows the user to convert static field accesses and static methods to use a static import. It's also possible to replace all occurrences at the same time.

More details here

Ankit Soni
  • 2,434
  • 2
  • 17
  • 19