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.
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.
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
Related
I am testing a Spring Boot application using code from a tutorial. The tutorial describes the setup and configuration of a Spring Boot application, and also describes a test that uses JAssert calls in the following manner:
package hello;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class SmokeTest {
#Autowired
private HomeController controller;
#Test
public void contexLoads() throws Exception {
assertThat(controller).isNotNull();
}
}
The test, unfortunately, will not compile in my IDE. The compilation is failing on the assertThat() method.
I am using Eclipse with Maven for my IDE. I have checked the Maven dependencies and see that the JAssert core library is included. Unfortunately, despite this the compiler can not seem to "find" the assertThat() call.
It fails to compile the test for that reason.
How do I get the test to utilize JAssert and find the calls to JAssert functions?
I'm not an eclipse expert, but it seems like you have some configuration issue here.
If you're sure that the dependency appears in pom.xml in scope test then try to eliminate eclipse related issues by running the tests directly via maven:
mvn test
will do the job
If it runs sucessfully, then re-create eclipse configurations from pom.xml / re-import the project.
If not, its a pom.xml related issue and has nothing to do with eclipse, you'll have to fix the pom or maven ecosystem. I suggest the following:
go to the local repository and remove the dependency from the file system (manually, the whole directory, with jar, pom.xml and everything) and then rerun mvn test
Sometimes dependencies are downloaded corrupted and despite being defined correctly in pom.xml they do not really contain classes in a form that java/maven can read
Actually:
It turns out that neither the Boot starter nor the IDE seems to find the necessary import declaration for the JAssert functions. Could it be because they are static?
In looking at some example code, I found in the code the import declaration for the assertThat() method. Usually, Eclipse would have suggested this declaration but it does not.
What is weird is that I put in the declaration by hand, and not only does this allow me to compile, but Eclipse, once the import is included, the code assistant makes proper assertThat() suggestions without problems!
There could be a bug somewhere, but that is beyond the scope of my particular problem. My test now compiles without problems.
I hope someone looking at this can figure out why Elipse's code assist does not work for this particular library properly. I wouldn't be surprised to discover that there are other objects/libraries in Spring Boot that don't get properly handled by the code assistant.
Whenever I use a Junit assert in my code, my IDE (Intellij IDEA 12) politely offers to static-import it for me:
import static junit.framework.Assert.assertTrue;
However, it always gives me the choice of importing either the "org.junit" version or the "junit.framework" version:
import static org.junit.Assert.assertTrue;
I can't find a clear answer online about what the difference is between these two packages - is there a difference? If so, what is it? They both come out of exactly the same Junit4 jar, so what's going on?
org.junit.* is JUnit 4+. The other is previous versions.
There's backwards compatibility, so junit.framework.* is included in junit-4.x.jar.
I have installed TestNG(ver:6.8.6.20130517_2218) in Eclipse (Ver: 1.4.2.20120213-0813) on Windows7(64bit). I verified that TestNG is successfully installed
When I write #Test in one of the sample jave file that I created, Annotation(#Test) is not resolved. It is giving Syntax error.What could be the issue? Please see the attached screenshot for more details
Make sure you are importing annotations.
import org.testng.annotations.*;
Thanks for all your answers and help. I got the mistake what I am doing:
I am just leaving by writing #Test annotation. Instead if I write the annotation along with function this error is not coming. For e.g.
#Test
public void sampletest() { }
then the error which I am facing won't come.
For any future visitors, I was also able to fix this issue by making the test method public.
testng annotations not working.
Inclusion of the following (post by Korey Hinton) helped.
import org.testng.annotations.*; -
TestNG only picks the Files with 'test' in the file Name or Public Class.
For Eg : the File name is App.java it won't run the #Test method but if we rename the file to AppTest it runs.
I am unable to import org.junit.Assert.AssertThat in my program. I am using Ganymede and jUnit 4.8.1.
Static Imports
It's org.junit.Assert.assertThat(T, Matcher<T>) and you can import it as a static import:
import static org.junit.Assert.assertThat
now in your client code you can do assertThat(something, ismatched())
Reference: Java Tutorial > The Static Import Statement
Regular Imports
To do it the old-school way, if you import the Assert class like this
import org.junit.Assert
you can call it using Assert.assertThat(something, isMatched())
(The isMatched() method is something that you'd have to implement)
assertThat()
assertThat() was first described in this blog post and has been part of JUnit ever since version 4.4, so make sure you have JUnit version 4.4 or newer on the classpath. Also, make sure that your compiler compliance level is 1.5 or higher:
The method is called assertThat (lower a, capital T). And if you import it like that you need to use a static import:
import static org.junit.Assert.assertThat;
But since you don't tell us the error message I can't really tell if that will work for you.
Assuming that by "i am using ganymede" you are stating that you are using the "ganymede version of eclipse", do the following:
Open the project properties.
Click on "Java Build Path".
Select the Libraries tab.
Click the "Add Library" button.
Choose junit.
You should now be able to import junit classes into your project.
I am trying to get a test class compiling with JUnit, and I can't figure out why it will not compile.
I have the following lines at the top of my class:
import java.util.*;
import org.junit.*;
And the error I am getting is
package org.junit does not exist
JUnit.jar is currently located in Program Files\JUnit\junit.jar, which currently also resides in my class path. I am working on Windows Vista if that helps.
Any ideas on how I can compile this test class with JUnit?
Thanks very much,
What version of JUnit are you using?
I think that until JUnit 3, the package was different:
import junit.framework.*;
Also, if you are using Eclipse, you can pick the JUnit framework to use.