useful import generation

I am fairly new to intellij and I still have not gotten over the frustration that it sorta does what I want but not exactly so I need to do it manually anyway.  I'm sure this is mainly my ignorance.  Here is a case in point.  I am writing a jUnit test case:

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;

public class ParameterizedTest {
    private double expected;
    private double valueOne;
    private double valueTwo;

    public static Collection<Integer[]> getTestParameters() {
        return Arrays.asList(new Integer[][]{
                {2, 1, 1},
                {3, 2, 1},
                {4, 3, 1},

I would like an easy way to get intellij add add the import to resolve @Parameters:

import org.junit.runners.Parameterized.Parameters;

Instead, all I can get it to do is change @Parameters to @Parameterized.Parameters.  That is technically correct but not what I want (if I wanted that, I would have typed that), so I end up having to add the import by hand.

Obviously this is a small thing, but it seems like it is always doing almost what I want but not quite.

Is there a way to tame it?



You have two options:

Option 1:
After initially entering the Parameters method:

import org.junit.runners.Parameterized;

public static Collection<Integer[]> getTestParameters()


If you place your cursor on the @Parameterized.Parameters and type Alt +Enter (to open the Quick Fix/Intentions dialog), you can select "Add static import for 'org.junit.runners.Parameterized.Parameters'". IDEA will then change the import statement for you to a static import andchange the reference:

import org.junit.runners.Parameterized;
import static org.junit.runners.Parameterized.*;

public static Collection<Integer[]> getTestParameters()


As a general note, always try Alt +Enter when doing things, especially routine things. You'll be amazed at how many Intentions IDEA has to do things. Just a few of the many examples:

  1. Change String concatenation to a StringBuilder (and visa-verse)
  2. Flip an if/else statement
  3. Flip the logic in a conditional
  4. Change the access level of a method or field
  5. and many, many more

If you go to Settings > [IDE Settings] > Intentions, you will see the list of them all. Some are configurable.

Alt +Enter is your BFF in IntelliJ IDEA ;)

Option 2
You can configure IDEA to always import inner classes. (This will apply to all inner classes, not just annotations)

  1. Go to File > Settings > [Project Settings] > Code Style > Java > [Import tab]
  2. In the top "General" pane, select the "Insert imports for inner classes" option
  3. Click OK

Now when you select @Parameters from code completion, optimize imports, or otherwise trigger an auto-import, IDEA will import the inner class:

import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.*;

public static Collection<Integer[]> getTestParameters()


In the same code style "import" dialog, you can configure certain packages to automatically be auto imported as static imports (or to always import a package rather than a class in the package). However, there is no way to have it auto import classes via a static import. There is a feature request to add this functionality. Please consider voting for it.


Many thanks for your help and time.  I deeply appreciate it.


Please sign in to leave a comment.