4
votes

in test classes I have the following import

import static org.junit.Assert.*;

when I do organize import via ctrl + shift + o then it automatically changes to following

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

How can I configure eclipse not to do it ? PS: I only want junit imports not be handled in that way

=============

I added a save action to remove unused imports. [properties -> java editor -> save actions] so everytime I save unused imports are removed (since I used ctrl + shift + o mainly to remove unused imports this looks like a way forward..)

2
Don't press "CTRL + SHIFT + O" then.midhunhk
:D @silverback. Whats wrong if eclipse does this. It's general practice to import only what is needed.Foolish
That is the correct approach. But the OP doesn't want it that way. :)midhunhk
The OP also wants to limit it to junit packages. Talk about wasting time being a control freak..Kayaman
@silverback thx for the hint.. seems like save actions will do the trick for meDev Blanked

2 Answers

8
votes

Change the number of static imports to 1

enter image description here

7
votes

Under Window, Preferences, Java, Code Style, Organize Imports there's an option called "Number of static imports needed for .*" - set that to 1. (Another way to find it quickly is just to type "static" into the search box in preferences.)

Note that this will mean that hitting Ctrl-Shift-O will always turn any static imports into an static import-on-demand form, which may not be what you want. If you have separate projects for test and non-test code, you could configure it on a per project basis.

Personally I'd just live with the explicit imports - I usually end up with static imports by starting off with the class-qualified call, and then hitting Ctrl-Shift-M on the method name to import it statically.