Missing "Run as JUnit Test" - java

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/

Related

Automatically run junit tests after build in NetBeans 8.0.1

I have a simple question. Is there a way to configure NetBeans so that after build JUnit tests in test package would run automatically ? I'm using NetBeans 8.0.1.
Thanks in advance.
If you are using Maven there's an option under Tools / Options / Java / Maven / Execution / Skip Tests for any build executions not directly related to testing. Make sure that this one is unchecked.
If you are using Ant this is also possible. It's been some years since I used Ant but from top of my head you need to modify your build.xml. There needs to be a target like "compile" and there you add in the depends-property the targetname for the unit tests.
For a project specific solution:
Right-click on your project -> Properties -> Actions.
Select "Build project" ("Debug project" and "Run project" work too!"
Add "surefire:test " at the beginning of the Execute Goals.
If you now share your source code with others, they will automatically run your tests too!

JUnit test case is not running in eclipse

I am using eclipse IDE in my java project, I have also configured "JUNIT" to my eclipse ide.
When I am running a test case in eclipse using,
"Rightclick On Project->Run As",
It is showing that "Run configurations".It is not showing "JUnitTestCase" .
What is the problem?
Look at the project facets by right clicking on the project. Is Junit added?
Also as long as you have added junit to your project classpath you should get the option.
Have you forgotten the #Test Annotation?

why is junit not not visible at all in my eclipse java compiler

I am relatively new in java programming specifically with eclipse environment. I am trying to use junit and tried the following
import org.junit.*;
however this is not working because junit is not part of the org package. nevertheless junit is installed on my computer. please could anyone tell me what could be wrong. from the java tutorials I have read so far, nothing is wrong with my code
You need to add the junit library to the classpath of your project.
In Eclipse you can do it like this:
right click on your project then go to
Build path > Configure Build path...
switch to the Libraries tab then click add Library. Choose JUnit and hit next, choose witch version you want, 3 or 4 (I recommend 4). Hit finish then hit OK.
That's it.
The JUnit JAR file is not included in the project build path.
Include the JAR file in project classpath for the current Eclipse project. Ensure the file is visible from the Package Explorer View. Then right click on the JAR file and select
Build Path > Add to Build Path
Alternatively, if you're using Maven, you can simply add the JUnit dependency your project's POM.XML file
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
Following onfrom reimus, For my version of eclipse (other version may differ):
Try right clicking on the package
Select build path
Select add libraries
Select JUnit
etc
You should then be able to use JUnit

Class Not Found Exception when running JUnit test

I am getting this error when running JUnit test in Eclipse:
Class not found com.myproject.server.MyTest
java.lang.ClassNotFoundException: com.myproject.server.MyTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
I've tried adding JUnit library in the classpath although I'm using maven, and the JUnit library is in the POM dependency.
I already have tried cleaning the project and created a new JUnit test case using the JUnit plugin for Eclipse, still getting the same error.
This appears to occur because only the source code is compiling when you use mvn clean compile (I'm using maven 3.1.0 so I'm not sure if it always behaved like this).
If you run mvn test, the test code will compile as well, but then it runs the tests (which may not be immediately desirable if you're trying to run them through Eclipse.) The way around this is to add test-compile to your Maven command sequence whenever you do a mvn clean. For example, you would run mvn clean compile test-compile.
I had a similar problem with my tests and found somewhere in the Web, that you have to go to Build Path in your project's properties and move Maven Dependencies above JRE System Library. That seems to have worked for me.
I faced the same problem and I was able to fix it using #slomek's answer but the issue resurfaced after that.
I finally fixed it by adding my project's output folder to the JUnit test's run configuration. Steps are:
Find your project's output folder from Project properties -> Java Build Path -> Default output folder
Usually this is <project-folder>/bin
Go to Run -> Run Configurations...
Click on the unit test's run configuration and click on the Classpath tab
Explicitly add your project's output folder under User Entries - even if the project is already included there
Click on Advanced -> Add folder to add the output folder
This issue might be due to weird project setup in Eclipse - e.g. source folder with nested maven projects imported as a single project/folder (at least that was how my project was setup).
If this problem occurs in Eclipse only, executing command Project -> Clean... on selected project may help.
After having tried everything here with no improvement, I solved my issue by just restarting Eclipse
I fixed my issue by running maven update.
Right click project your project > Maven > Update Project
In my case I had a wrong maven directory structure.
Which should be like:
/src/test/java/ com.myproject.server.MyTest
After I fixed that - everything worked like a charm.
In my case, only next steps helped me to resolve this issue:
Project->properties->Run/Debug Settings.
In "Launch configurations for '.....'" select classes/projects
Edit -> Classpath -> "Restore Default Entries"
I had the similar problem with my Eclipse Helios which debugging Junits. My problem was little different as i was able to run Junits successfully but when i was getting ClassNotFoundException while debugging the same JUNITs.
I have tried all sort of different solutions available in Stackoverflow.com and forums elsewhere, but nothing seem to work. After banging my head with these issue for close to two days, finally i figured out the solution to it.
If none of the solutions seem to work, just delete the .metadata folder created in your workspace. This would create an additional overhead of importing the projects and all sorts of configuration you have done, but these will surely solve these issue.
Hope these helps.
Earlier, in this case, I always did mvn eclipse:eclipse and restarted my Eclipse and it worked. After migrating to GIT, it stopped working for me which is somewhat weird.
Basic problem here is Mr Eclipse does not find the compiled class.
Then, I set the output folder as Project/target/test-classes which is by default generated by mvn clean install without skipping the test and proceeded with following workaround:
Option 1: Set classpath for each test case
Eclipse ->Run ->Run Configurations ->under JUnit->select mytest -> under classpath tab->Select User Entries->Advanced->Add Folder -> Select ->Apply->Run
Option 2: Create classpath variable and include it in classpath for all the test cases
Eclipse ->Windows ->Classpath Variables ->New->[Name : Junit_test_cases_cp | path : ]->ok
Then go to
Eclipse->Run ->Run Configurations ->JUnit->select mytest ->under classpath tab ->Select User Entries->Advanced->Add classpath variables->Select Junit_test_cases_cp->ok->Apply->Run
This is the only thing currently working for me after trying all the suggestions online.
NoClassDefFoundError really means it can't initilize the class. It has nothing to do with finding the class. I got this error when calling trim() on a null String.
JUnit won't show NullPointerException. The string isn't null when running normally because
I'm fetching the string from a properties file which is not availible for tests.
My advice is to remove pieces from the class until your tests start passing. Then you can determine which line is giving the error.
In my case, changing the order of Maven Dependencies from Build Path configuration did not work for me. I changed its order from Run configuration.
Follow these steps:
• Go to Run -> Run Configurations...
• Click on the unit test's run configuration and click on the Classpath tab.
• check Use temporary JAR to specify classpath (to avoid classpath length limitations).
May be it is enough and your test works. You should try, so press Run button. If it did not work. Follow all previous steps again and without pressing Run button, go to the next step.
• In sub branches of User Entries move Maven Dependencies above your project or test class.
• Click Run button.
These steps worked for me.
Delete the content of local Maven repository.
run mvn clean install in the command line. (cd to the pom directory).
Build Project in Eclipse.
1- mvn eclipse:eclipse
2- project clean all projects
3- restart
It's worth mentioning as another answer that if you're using eGit, and your classpath gets updated because of say, a test coverage tool like Clover, that sometimes there's a cleanup hiccup that does not completely delete the contents of /path/to/git/repository/<project name>/bin/
Essentially, I used Eclipse's Error Log View, identified what was causing issues during this cleanup effort, navigated to the source directory, and manually deleted the <project name>/bin directory. Once that finished I went back to Eclipse and refreshed (F5) my project and the error went away.
It can also be due to "[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?"
I had the same problem with a Gradle project with a test SourceSet with two resource directories.
This snippet comes from a main-module.gradle and adds a resource dir to the test SourceSet:
sourceSets {
test {
resources {
srcDir('../other-module/src/test/resources')
}
}
}
Doing this I had two resource directories related to the test SourceSet of the project main-module:
../other-module/src/test/resources
src/test/resources (relative to the main-module folder, automatically added by the java plugin)
I find out that if I had two files with the same name in both the source directories, something in the process resources stage went wrong. As result, no compilation started and for this reason no .class were copied in the bin directory, where JUnit was looking for the classes. The ClassNotFoundException disappeared just renaming one of the two files.
It seems compile issue. Run project as Maven test, then Run as JUnit Test.
For project that does not use maven : This worked for me https://ihategeek.wordpress.com/2012/04/18/eclipse-junit-test-class-not-found/
Adding the jre and project src at the bottom in Order and exports in build path
Pls check if you have added junit4 as dependency.
e.g
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
For me I had to put the project x/src/test/java/ at the bottom of the "order and export" in the "java build path"
Check if your project is opened as a Maven project and not just a regular Java project. Actually a no-brainer, but that is exactly the same reason why you might miss it.
I had faced the same issue. I solved it by removing the external JUnit jar dependency which I added by download from the internet externally. But then I went to project->properties->build path->add library->junit->choosed the version(ex junit4)->apply.
It automatically added the dependency. it solved my issue.
Making some dummy change and saving the test class can solve the problem. It will build the .class automatically
Might be you forgotten to place the Main class and Test Case class in
/src/test/java. Check it Once.

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