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.
Related
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 am importing this following :
import org.apache.lucene.analysis.PorterStemmer
in Java program. The whole package is available in refrenced library.
I tried importing
import org.apache.lucene.analysis.PorterStemFilter
and
import org.apache.lucene.analysis.Analyzer;
both are working fine except the first one mentioned
Can anybody point out why ?!
Package org.apache.lucene.analysis.PorterStemmer is not a public package which is why you cannot import it. If you look at this package inside the library, you'll notice that it begins with class PorterStemmer instead of public class PorterStemmer.
My guess is that you have a different version of the Lucene JAR that doesn't contain the class that's failing to work. Open the JAR with WinZip, 7Zip, or some other tool and see if that class is indeed missing. If it is, you either need to find a version of the JAR that has it or rewrite your code to use an alternative.
I need an object of javax.servlet.jsp.JspWriter in my JAVA program. I have to import javax, but I do not know how to do it.
I use ant for compiling my project and no IDE, just commandline.
Should I download a package or something and put it in my project? Or there is another way?
An IDE would ease thing considerable. Ant intergrates with Eclipse and helps to ease management of imports.
For the commandline; download the appropriate jar, add it to the classpath using -Djava.ext.dirs=
I presume that you can take it from here!
To import javax.servlet.jsp.JspWriter you place an import statement at the top of your file, after the package declaration, but before the class declaration. The import statement would look like:
import javax.servlet.jsp.JspWriter;
This tells the compiler to interpret uses of JspWriter as javax.servlet.jsp.JspWriter
For example:
package somepackage;
import javax.servlet.jsp.JspWriter;
public class AClass {
//Class contents here
}
I need an object of javax.servlet.jsp.JspWriter in my JAVA program. I
have to import javax
No you don't, you have to import javax.servlet.jsp.JspWriter. So do that. If it doesn't compile, adjust your classpath or your IDE project settings to include the appropriate JAR file.
I'm working on a project and need to use the Predicate interface of google's common.base
I tried importing the google-collect jar at http://code.google.com/p/gdata-java-client/source/browse/#svn/trunk/java/deps
I got the jar, imported it in my library but nothing's there...
Can anyone tell help me to be able to work with this interface?
If you are using eclipse, you need to add the library jar to your build path, as described here.
If you are compiling from the command line, use the -classpath option to add the jar to the path.
Also make sure the case is correct in your import declaration. The import should look like
import com.google.common.base.Predicate;
not
import Com.Google.Common.Base.Predicate;
Also you probably know this already, but the import com.google.common.*.* suggested by your question title is invalid - you can only have one *. (import com.google.common.base.* is OK.)
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