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;
@RunWith(Parameterized.class)
public class ParameterizedTest {
private double expected;
private double valueOne;
private double valueTwo;
@Parameters
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?
Thanks!
Please sign in to leave a comment.
You have two options:
Option 1:
After initially entering the Parameters method:
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:
<side-bar>
<new-user-tip>
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:
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 ;)
</new-user-tip>
</side-bar>
Option 2
You can configure IDEA to always import inner classes. (This will apply to all inner classes, not just annotations)
Now when you select @Parameters from code completion, optimize imports, or otherwise trigger an auto-import, IDEA will import the inner class:
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.