Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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?

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

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 http://img18.imageshack.us/img18/7906/screenshotmavenpowermoc.png

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top