Enable static import wildcards for some classes in Intellij IDEA - java

I want to enable static import wildcards for classes usually used in tests, e.g. org.hamcrest.Matchers.
So, when I'll execute the Optimize Imports action instead of this :
import static org.hamcrest.Matchers.any;
import static org.hamcrest.Matchers.is;
I would get this :
import static org.hamcrest.Matchers.*;
Is it possible to do that in IDEA ?
Solution
Settings :: Editor :: Code Style :: Java => Imports.

Related

Junit5 assertions import

So, I included Junit5 to my maven project and it kind of works, but anyway when I try to import assertions to a test I can only put org.junit.jupiter.api.Assertions.* without beign able to specify some precise assertion and in the code I have to write for example Assertions.assertThrows().
Why can't I pick any specific assertion from the list?
What could be the problem?
If you only want to import a single method, you have to use a static import, like this:
import static org.junit.jupiter.api.Assertions.assertThrows;
Then you can simply use it like:
assertThrows(Exception.class, () -> ...)
You can import specific assertions by using a static import:
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
This works for me in eclipse…

eclipse java import organizing

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..)
Change the number of static imports to 1
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.

static import only from classes and interfaces

My code compiles fine in Eclipse, but when I try to compile from the commandline (via our ruby-based buildr system), I get this error message:
static import only from classes and interfaces
Suggesting that static import of public static fields is not permitted. What should I look for to help diagnose this problem? How can I fix it?
Update:
per #Ted's request, the constant declaration in the referenced file:
public static final String NULL = "<NULL>";
and the (bowdlerized) reference in the referring file:
import static my.path.MyClass.NULL;
My guess is that Eclipse and buildr are using either different Java compiler versions or different compiler flags. There's a bug in the Java 7 compiler (bug ID: 715906) that generates this error when you statically import specific fields. The work-around is to use a wildcard static import. So instead of:
import static pkg.Class.staticField;
do this:
import static pkg.Class.*;
Late answer but I just got a similar issue and figured it out. I'll post in case it helps anyone else who finds this page...
I got a similar error when, after a big merge and refactor, I accidentally put a test class into src/main/java instead of src/test/java. Since the JUnit dependency was scope=tests, it didn't work in pure maven. Maybe you are having the same issue
I also had this error and my issue turned out to be a wayward static import of a junit 4 package in my test source file.
I had the following:
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeout;
I removed the import static org.junit.Assert.fail; (no idea how I managed to get that in there in the first place) and all is now working.
I accidentally set test directory as source. And Test sources were considered as source files.
sourceSets.main.java.srcDirs 'src'
| -- src
  | -- main
  | -- test
Fix:
sourceSets.main.java.srcDirs 'src/main'
Some how same solution mentioned by #m-watson
I have replaced
import static org.junit.Assert.assertThrows;
With
import static org.junit.jupiter.api.Assertions.assertThrows;
and it worked

Static import - Netbeans error

The error, Netbeans gives me, is:
static import only from classes and interfaces
which is somehow strange for me, as this:
import org.lwjgl.opengl.GL11;
works fine while this:
import static org.lwjgl.opengl.GL11;
doesn't. Why is it not working for me?
BTW, GL11 is a class and I don't know why but Netbeans, when importing statically, thinks opengl is the class I want to import.
You wanna write:
import static org.lwjgl.opengl.GL11.*;
You are importing the members of the class, thus the * at the end.
Static import allows you to import static fields of other class. For example you can say
import static java.awt.Color.RED;
And then use RED in your class without mentioning class where it is defined.

Finding import static statements for Mockito constructs

I'm trying to crash through the brick wall between me and Mockito. I've torn my hair out over trying to get correct import static statements for Mockito stuff. You'd think someone would just throw up a table saying that anyInt() comes from org.mockito.Matchers and when() comes from org.mockito.Mockito, etc., but that would be too helpful to newcomers, no?
This sort of thing, especially when mixed in with myriad more import statements ending in asterisks, isn't always very helpful:
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
Yes, I know about and have been trying to use the Eclipse Window -> Preferences-> Java -> Editor-> Content Assist -> Favorites mechanism. It helps, but it doesn't hit the nail on the head.
Any answers to this question would be appreciated.
Many thanks,
Russ
Here's what I've been doing to cope with the situation.
I use global imports on a new test class.
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.Matchers.*;
When you are finished writing your test and need to commit, you just CTRL+SHIFT+O to organize the packages. For example, you may just be left with:
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.anyString;
This allows you to code away without getting 'stuck' trying to find the correct package to import.
The problem is that static imports from Hamcrest and Mockito have similar names, but return Matchers and real values, respectively.
One work-around is to simply copy the Hamcrest and/or Mockito classes and delete/rename the static functions so they are easier to remember and less show up in the auto complete. That's what I did.
Also, when using mocks, I try to avoid assertThat in favor other other assertions and verify, e.g.
assertEquals(1, 1);
verify(someMock).someMethod(eq(1));
instead of
assertThat(1, equalTo(1));
verify(someMock).someMethod(eq(1));
If you remove the classes from your Favorites in Eclipse, and type out the long name e.g. org.hamcrest.Matchers.equalTo and do CTRL+SHIFT+M to 'Add Import' then autocomplete will only show you Hamcrest matchers, not any Mockito matchers. And you can do this the other way so long as you don't mix matchers.
For is()
import static org.hamcrest.CoreMatchers.*;
For assertThat()
import static org.junit.Assert.*;
For when() and verify()
import static org.mockito.Mockito.*;
My imports
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
And it works
import static org.mockito.Matchers.anyInt;
e.g.
when(listMock.get(anyInt())).thenReturn("stackoverflow");

Categories

Resources