geeky · java

Eclipse 3.6 Auto Static Import

After I started using JUnit 4, I really want Eclipse to automatically import org.junit.Assert.* statically for me. So when I do ctrl+space on methods like assertTrue, it will do:

import static org.junit.Assert.*;

for me.

I’ve figured it out for a while but I’ve been noticing it conflicting with my save action -> organize imports setting.

I finally got fed up and decided to investigate a bit further today.

Below are screenshots of Eclipse version 3.6.

  • Preferences -> Java -> Editor -> Content Assit -> Favorites all of the paths you wish to import statically e.g. org.junit.Assert.*
  • Then if you have Preferences -> Java -> Editor -> Save Actions -> Organize imports selected

    Make sure you update Preferences -> Java -> Code Style -> Orangize Imports to have Number of static imports needed for .* (e.g. ‘java.lang.Math.*) to 1.

    This way when you save your java files in Eclipse, it will not change your org.junit.Assert.* import to org.junit.Assert.assertTrue import. Otherwise it will require you to import again if you wish to use another method like assertFalse which in my opinion is annoying.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s