Failed to import junit - java

Is it a bug in Eclipse? I have tried to refresh the project, restart Eclipse and system. Confused.

Is it a bug in Eclipse?
Nope. It is a bug in the project setup. It looks like the project's maven dependency for junit is incorrect.
You are using the wrong Junit JAR file for the test code you are trying to compile. The junit.framework.TestCase class is from Junit 3, but you are using the JAR file for Junit 4.7. In Junit 4.x the package names changed to org.junit, and the TestCase class was dropped.
Solutions:
Find and use the JAR file for the last Junit 3.x release. (This might be what you need ...)
Update your testcases to use a newer version of Junit. (Preferably Junit 5.x)
(At the time of writing, the latest release of Junit 4.x is 4.13.)

Related

Running junit tests in Intellij with Java 6 & lombok annotations

Could someone please suggest me the required Intellij Idea setup to run a junit test refers to lombok annotations with Java 1.6.
My current setup:-
Intellij Version - 2020.3.3 with built-in lombok plugin
lombok dependency version - 1.16.18
My previous question on the issue:-
lombok annotations are not identified by junit in Intellij
I can build the project with maven but cannot run unit test with Intellij. When I changed to Java 8 and try to run unit tests, lombok error disappeared, but failed since project is not compatible with Java 8.
Therefore I'm looking for the ideal Intellij setup that works for this situation.
Thanks in advance!

The import org.junit.jupiter cannot be resolved

Hey its the first time i use JUnit with Java and i'm not sure how to set it up inside Visual studio Code. I tried to put Junit-4.13.jar inside my a lib file in the root directory but nothing changed. any ideas? I am on MacOS.
There were 2 main issues with this configuration:
JUnit 4 dependency jar was used while in the source code imports from JUnit 5 API were used
Tests resided in the same location as sources, while the dependencies defined via Gradle/Maven for tests limit the scope to src/test/java roots.
In a non-Gradle project the issue was fixed by adding the proper JUnit 5 dependencies to the module.
In the Gradle project the issue was fixed by moving the test class from src/main/java to src/test/java.
Sample Gradle project is available on GitHub.
More details about using JUnit 5 can be found in the official documentation.

Eclipse 2018-09 with jdk11 claims that package org.junit is missing even though it is included by maven

my maven project includes junit and builds including running the tests on the command line with maven. However eclipse claims that the package can not be found, I don't understand why eclipse would ignore the dependency.
I am depending on version 4.12 of junit
Right-click on the project, then use Maven>Update Project... to get the build path settings in sync with the pom.xml.
Please check the version of junit in the pom.xml.
The 3.x version is programmed and 4.x is the annotation method.

Missing "Run as JUnit Test"

I created a JUnit 4 test in Eclipse by right-clicking on a Java class and selecting New JUnit Test Case. When I right-click the test class I get "Run on Server", but not "Run as JUnit Test". I am using Eclipse 3.6.1.
In my case, Eclipse must have reached a corrupt state. Restarting Eclipse fixed the problem.
I think I see the problem. You need to have an actual test in the file before Eclipse identifies it as a test case. Try inserting the following:
#Test
public void foo() {
}
for junit5 jdk8 is needed in build path and project facets.
Make sure your class has JUnit traits (extends from TestCase, or use #Test etc);
Right-click "Run As" -> "Run Conciguration" -> Create JUnit test from left icon "JUnit" anyway;
check whether you have jave 8 or above and configured correct jdk in build path.
check at least #Test is used in your test case class file
Then go to run configuration->select Junit->right click and new configuration->browse the package->select junit5 as test runner.
In case you experience it in maven project add following into pom.xml and reload project using Alt+F5. Tests do not work because old JDK version.
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Make sure to have a valid default constructor for your test class.
In my case, the problem was different. I was converting a TestNG test to JUnit. The #Test annotation was satisfied by the TestNG import, but that was the wrong annotation. I removed the TestNG import and added the JUnit import for #Test, and the right-click menu option to run as a JUnit test appeared.
The eclipse shortcut to run Junit test is Alt+Shift+X, T.
If its not working just press Alt+shift+X a menu will popup just look for Junit.
I ran into these symptoms when importing an existing project into a Kepler-based Eclipse IDE for Java Developers version.
Importing the project into a Luna-based Eclipse IDE for Java EE Developers correctly set set it to a Java project (project icon now includes that little J) and now allows running JUnit tests.
In my case the Java Build path (.classpath file) was corrupt. In particular it had a merge conflict which was unresolved. Therefore, the JUnit 4 library was missing.
I used to have a similar problem and it turned out that was because I forgot "extends Specification" after the "ClassToBeTestedSpec" in the declaration.
Make sure that you include import org.junit.Test; and the tests have #Test before them, I have missed that before when coping some functions from other files.
To solve the situation, a restart of eclipse wasn´t enough in my case, I had to remove and re-add the project to my workspace as well.
If, the configuration is of SpringRunner.class for Junit.
Make sure your maven clean install with maven update and refresh is run in your project with right jdk and mavens password/configuration.
This recurred for me, this time it was that Junit simply wasn't added to the classpath (but it has in the past been). This is the first Eclipse project for me to use Junit 5. Maybe that has something to do with it? I know this is a very old post, but I think this might be needed for more recent reasons.
Right-click your project and choose 'properties'
choose Build Path -> Configure Build Path
Click on Libraries tab.
Click on Add Library button.
Click on JUnit and Next button.
Choose JUnit version. (for me, Junit 5)
Click Finish.
Info From
https://www.eclipse.org/forums/index.php/t/304679/

Why is my Mockito-based unit test unable to run in Eclipse?

I have a Maven-managed project that uses Mockito mocking in its unit tests. I can run all the tests within a Maven build, and they run without error (and pass!). However, if I right-click a single test function, and choose "Run As -> JUnit Test", I get an exception java.lang.NoSuchMethodError: org.mockito.Mockito.doAnswer(Lorg/mockito/stubbing/Answer;)Lorg/mockito/stubbing/Stubber;. Of course, the "missing" method is there if I look at the sources, and like I said the tests compile and run from the command line.
Best I can think of is if Eclipse is trying to "help" me by providing an outdated Mockito artifact (I'm using 1.8.5 in my Maven dependencies) for the JUnit plugin, akin to how the Maven plugin can stick you with an oddball version of the Maven runtime for certain tasks.
Is this the problem? Is it something else? Can I fix this?
ETA: Apparently this may relate to a known issue. There's a good chance that it stems from having multiple versions of Mockito in my classpath (thanks, Maven :-/ ). I seem to have my house in order -- Eclipse can run the tests now -- but unfortunately the bug has bitten my Hudson. I have to track down how to remove the old JAR from the classpath there as well.
Make sure the unit-test classpath has the correct mockito. You can check this from the run dialog. Btw, Eclipse does not ship with mockito, so perhaps you are having two versions of it. Take a look at your maven dependency graph and search for duplicates.
I had the similar problem and I found that I had both "mockito-all 1.8.x" and "mockito-core 1.9.5" in my classpath. I was supposed to use only 1.9 but somehow eclipse was putting 1.8 before 1.9.5 in the classpath. I removed 1.8.x and it worked ;)

Categories

Resources