views:

269

answers:

6

I've noticed that when importing JUnit, the * wildcard doesn't always work.
e.g. for the annotation @Test you must import org.junit.Test since org.junit.* doesn't recognize the annotation.

Is there a reason for this, is it something that needs setting? or just a quirk in the way somethings like JUnit are.

FYI, I am using: Junit 4.6, Intelli-J 8.1.3.

A: 

There's no reason I know of why importing org.junit.* wouldn't give you access to org.junit.Test. In fact, I just tried it in Eclipse, and it works there. Perhaps it's a problem with your IDEA workspace?

Kaleb Brasee
Would suggest that there's a setting for it or a larger issue that is my problem. I find it hard to believe that Intelli-J is simply broken in that respect.
Xenorose
A: 

I don't do it, but using import org.junit.*; works fine here, the following test turns on a green light:

import static junit.framework.Assert.*;

import org.junit.*;

public class AppTest {
    @Test
    public void testApp() {
        assertTrue(true);
    }
}

Tested with Java 6u16 on the command line, under Eclipse 3.5, under IntelliJ IDEA 9.0 BETA CE. Works everywhere as expected.

alt text

Pascal Thivent
In which IDEA does it work for you? I've copy-pasted it and got "annontation type expected".
Xenorose
A: 

Works fine for me in IntelliJ. Something screwed up in your settings?

irreputable
Nothing that I changed, any idea what could it be?
Xenorose
glitch happens. why not try removing and adding back the junit jar? restarting the program? rebooting the machine? etc etc.
irreputable
Have tried all of the above, still problem persist.
Xenorose
A: 

I had a similar problem today in Eclipse. I made a static import to org.junit.Assert.assertEquals, but a static import of org.junit.Assert.assertThat fails! And they are in the same class!

I'll bet it's an Eclipse bug. I'm using junit 4.4 and eclipse 3.5

David Roussel
I use Intelli-J (as noted in the tags of this question), so it's not just Eclipse.
Xenorose
+1  A: 

Based on your comment above:

I've copy-pasted it and got "annontation type expected".

it sounds to me like it could be a name collision. Are you importing a class or interface named Test from somewhere else? Is there a class named Test in the same package as the one where you're having the problem? It could be that Java is seeing one of these instead of the annotation.

Moss Collum
"Test" is a *very* generic name, and I suspect a name collision as well.
Joe Carnahan
+1  A: 

I'm reading something at http://www.velocityreviews.com/forums/t369296-p2-disadvantage-of-using-wildcards-in-import-statement.html that suggests that there's an "optimize imports" setting in IntelliJ that might relate to this.

Jim Kiley