Why can't java find the JUnit framework? - java

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.

Related

JAssert library not incorporated in Spring Boot test

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.

How to get JUnit 4.11 to recognize class in default package?

Similar to other questions on this subject, I am having problems getting JUnit 4.11 to recognize my class.
Similar to the question found here, I am using algs4.jar to write a class, Percolation, which is all in the default package. Just as in the post I linked, I too had difficulty importing algs4 into my class to use the required WeightedWuickUnionUF class found within. I was finally able to compile the program with the following setup.
> echo $CLASSPATH
/Users/dj/Library/Java/Extensions/algs4.jar:
/Users/dj/Library/Java/Extensions/hamcrest-core-1.3.jar:
/Users/dj/Library/Java/Extensions/junit-4.11.jar:
/Users/dj/Library/Java/Extensions/stdlib.jar
Percolation.java
import java.util.Arrays;
import algs4.*;
public class Percolation {
private WeightedQuickUnionUF union_find_ds;
...
}
test_Percolation.java
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.Assert.*;
#RunWith(JUnit4.class)
public class test_Percolation{
private static Percolation perc;
#BeforeClass
public static void testSetup(){
perc = new Percolation(5);
}
#AfterClass
public static void testCleanup(){
perc = null;
}
#Test(expected = IndexOutOfBoundsException.class)
public void testIndexErrorThrown(){
perc.open(0,5);
}
}
Compilation was accomplished with the following commands. Both .java files are in the same folder, so too is a directory titled algs4.jar with the algs4.jar fully expanded such that each class exists as its own file. Therefore in the working directory there are the following files.
Percolation.java
test_Percolation.java
algs4/WeightedQuickUnionUF.class
I feel that this is an important point because even though algs4.jar is within the classpath, the Percolation.java file ONLY compiles when I have algs4/WeightedQuickUnionUF.class in the current working directory. For some reason I cannot get the import to work with the .jar in the classpath.
>javac Percolation.java
>javac test_Percolation.java
Please note that in Percolation.java if I changed the import line from import algs4.*; to import algs4.WeightedQuickUnionUF; I receive the following error at compile time.
Percolation.java:23: error: cannot access WeightedQuickUnionUF
import algs4.WeightedQuickUnionUF;
^
bad class file: ./algs4/WeightedQuickUnionUF.class
class file contains wrong class: WeightedQuickUnionUF
Please remove or make sure it appears in the correct subdirectory of the classpath.
Having compiled both files successfully, I attempted to use the solutions found from the following question. Entering the command java org.junit.runner.JUnitCore test_Percolation or the more verbose but seemingly unecessary command java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore test_Percolation both yield the following error.
JUnit version 4.11
Could not find class: test_Percolation
Time: 0.002
OK (0 tests)
I suspect that there is something wrong with my system configuration on top of the effects of my limited knowledge of Java and importing using Java. I know these questions have been posted elsewhere, but the solutions to said questions have not worked on my machine. Perhaps I have missed a nuance of the answers to other questions.
I am using Mac OSX 10.8.5 doing all of the programming and compilling from the command line using javac 1.7.0_07.
Look at your command-line:
java -cp /Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore
You've got the JUnit jar file on your class path - but not the current directory. So it won't be able to find any classes that aren't either "system" classes or in JUnit.
Try something like:
java -cp .:/Users/dj/Library/Java/Extensions/junit-4.11.jar org.junit.runner.JUnitCore
Ideally you should also start using packages though - and using more conventional class names. (test_Percolation violates normal conventions.)

Java library class not recognized?

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.

Unable to import org.junit.Assert.AssertThat;

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.

Junit import using * wildcard

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

Categories

Resources