JUNIT Test class in Eclipse - java.lang.ClassNotFoundException
Asked Answered
A

27

59

I'm trying to run my junit test (to verify that a properties file loads correctly) but I get ClassNotFoundException although the class is there and all required libraries are there too.

Here it is the error I get :

Class not found ConfigurationManagerTest                                                 java.lang.ClassNotFoundException: ConfigurationManagerTest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    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)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

any ideas on how to fix this ?

Thanks.

Auramine answered 19/4, 2011 at 12:26 Comment(7)
try clean & build your project.Spurious
tried it many times , i keep getting the same errorAuramine
Open the Run Configuration window in eclipse, select the unit test you ran and go to tab classpath: here you can check what is actually in the classpath.Staats
thanks, i saw an error above in the classpath tab : [JRE]: JRE not compatible with workspace .class file compatibility: 1.6 , maybe this the cause ?Auramine
I faced the same problem with a standalone Java class and a JUnit test, using Eclipse 4.2. I checked all paths etc. I tried "clean and build your project". It worked! Thanks Harry Joy.Carbolated
Do not use the eclipse gradle plugin. Use the gradle command to generate eclipse project files by (first deleting .project .classpath .settings) adding the eclipse plugin to build.gradle then running 'gradle eclipse' and importing a general project into eclipse.Tema
refresh your project and then do clean and buildWhiteman
M
41

ConfigurationManagerTest is not being found on your classpath. Ensure that the ConfigurationManagerTest.class file is available on your classpath.

It might not exist if it wasn't successfully compiled or if it's being created in a directory that you haven't told the Eclipse project should be on the classpath.

Assuming that you've put your test classes in a separate folder, make sure that it shows up in the "Package Explorer" view (rather than the "Navigator" view).

Under the project properties, "Java Build Path" and the "Source" tab, you can also see if the source folder is included for building as well as where the .class files are generated.

Mischiefmaker answered 19/4, 2011 at 15:5 Comment(4)
Adding some more on missing class files: Check if you see class files on the <my project>\build\classes\ dir. If you don't see any, check Eclipse's problems pane for errors about building your tests project. Fix those and it will build. If all else fails you might need to resort to Project->clean, then re built. And if you're really desperate restart Eclipse (A long shot).Cincinnatus
I noticed in my eclipse project's setup that all of the build classes were not going into the default project build dir, so my tests couldn't even access the build artifacts.Becki
yes, my maven project was building .class files in /bin. had to add that dir under user entries in runconfig / classpath tab via advanced/add folder option.Receivership
This worked for mo like a charm!! I have many (future) projects in my workspace that have compile issues. Adding the test class to the classpath ran the test that I needed.Immigrant
G
25

There is one more possibility. I had the same problem just now and no one of the solutions here helped. Except removing and recreating of the project - I didn't want to try it. What did help, was to clean the project two times immediately one after another! Clean + build could be repeated any number of times - it won't help. Only clean+clean and after that build goes OK. (Eclipse 3.6). Of course, you should disable autobuild for that.

Edit: This post has got its last plus on 15.11.2017. So, the problem (and the solution) remains actual.

Gannon answered 14/3, 2013 at 11:50 Comment(14)
Thanks, that helped, but I had to disable automatic build. Also I had to solve other missing libraries unrelated to jUnit.Dip
Did not help at all (Eclipse 4.5)Ullyot
@ChristianVielma I hope so. During 9 versions evolution they should remove the bug!Gannon
two times! that's Eclipse.Hannus
One time, I cleaned the project and i could now run my tests. Thank you.Cheatham
In my case it helped to clean the project once, but disabling "build automatically" prior to cleaning the project.Driveway
JUnit suddenly started failing with this issue. Restarting eclipse didn't help. Single clean didn't help Double cleaning did the trick: "Insanity is doing the same thing over and over again and expecting different results - unless you're using Eclipse".Bicameral
@naoru It is sad that they didn't remove the bug till now, but you can improve my mood by upvoting :-)Gannon
A plus does not mean the problem remains, because someone can still use an old version of eclipse etc.Soughtafter
@Code.IT I think, hardly MZD and naouru would write their comments if they used an old version?Gannon
Or they just give ya a plus because you discovered the same problem ;-)Soughtafter
@Alessandro the magic is that they have the same bug for 4 years already. I am measuring the life length of the bug counting upvotes :-)Gannon
Had the same issue on the same project both in VS Code and IntelliJ. Twice reproduced, twice resolved by doing this. It may not be an Eclipse issue after all. Now I clean 3 or 4 times to be sure.Disorder
@Disorder Don't forget, that similar behaviour can be caused by different errors. Definitely, it is about your case, for the Eclipse problem repeated in many cases, but always it was accidental, and never reproducible... But the cause of these errors in Eclipse and IJ is definitely the same - bad or even absent analysis. The clean state of a project must be absolute, the very fact that another cleaning somehow changes the project is the sign of a very bad, conceptual error, that can cause errors of any seriousness in future, even if the recently found change is negligible.Gannon
C
4

Another possible problem is a missing builder (it will prevent from your .class file from being built).

Check that your .project file has the following lines

<buildSpec>
  <buildCommand>
    <name>org.eclipse.jdt.core.javabuilder</name>
    <arguments>
    </arguments>
  </buildCommand>
</buildSpec>
<natures>
  <nature>org.eclipse.jdt.core.javanature</nature>
</natures>
Coleslaw answered 12/4, 2012 at 18:55 Comment(1)
This was my problem - forgot that my project was AspectJ enabled and so had the AJDT builder, which I didn't have installed. It's annoying that Eclipse will quietly refuse to build the project in these cases.Christychristye
D
3

This happened to me when I was dragging and dropping test classes to different packages. I just did the following

  1. Saved the class in a text editor.
  2. Deleted the offending class from eclipse project explorer.
  3. Re-created the class.

Bingo! I can now run the test!

Delphadelphi answered 17/4, 2012 at 14:11 Comment(2)
Great! None of my tests was compiling anymore (no .class files). I deleted and recreated only one class using your method, and suddenly all test classes compiled again.Durgy
Did not help at all in my case (Eclipse 4.5)Ullyot
M
2

I had this problem and it was kind of tricky to realise what was wrong. The project had a dependency project with some error, which stopped the build from execute. When I remove this dependency problem, the project was built as expected.

Ps.: I am working on a project that has many compilation errors, because we are porting an application that was converted from Delphi to Java, so I didn't care to the compilation error at the beginning, that's why it took me some time to find out the problem.

Meilhac answered 17/2, 2013 at 17:9 Comment(0)
P
2

I tried all the answers described here but none worked, but found this thread where slomek solves the problem in a very easy manner. Just go to project -> properties --> java build path. Then move Junit to the top by hitting the up bottom to the right. Then everything compiles just fine.

Poachy answered 12/4, 2013 at 13:34 Comment(0)
T
2

check properties->java build path -> libraries. there should be no errors, in my case there was errors in the maven. once I put the required jar in the maven repo, it worked fine

Theroid answered 29/5, 2013 at 4:31 Comment(0)
I
1

I tried everything mentioned here and in other posts. Some of the solutions that people proffered were:

  1. Change the output folder for the test
  2. Create a custom builder for the project that would run test-compile from Maven
  3. Move the Maven dependencies higher in the Order and Export list in the project build path

There were many, many more but the one that I found to work was as follows: Close the development environment. Delete the jars used by the project from my local Maven repository. Open the IDE. Build the project. Run the test.

After hours of beating my head against my keyboard and following suggested solutions, this one worked!

Insist answered 17/5, 2013 at 14:59 Comment(0)
H
1

what worked for me is to remove the runconfiguration of the test. Then right click the testclass and click run as junit test.

now it recreates a correct run config for me.

Hemminger answered 25/12, 2013 at 16:4 Comment(0)
O
1

Another way that this can unfold, and just did for me, is if you have a build error that Eclipse doesn't tell you about. If compiling the unit test fails then there is no .class file and you will get the ClassNotFoundException.

In my case there was a missing 3rd party jar file. When I ran the test I got a pop-up window that said "Errors exist in required project(s)". I work with a huge project and I always get that message because some of the source is not available to eclipse (long story). Also it doesn't say what the errors are. So I click "Continue" and then I get the exception.

Once I realized what was going on it was easy to fix by adding the missing jar to the classpath under Run -> Debug Configurations...

I'm not sure how to best detect or prevent this from happening except to be aware of this possibility when something goes wrong and to review your most recent changes for what might have gone wrong.

Ontologism answered 16/9, 2015 at 18:59 Comment(0)
U
1
I also faced similar issue as mention in comment below.
    
    **org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:766)**
    
Check all mention possibility above but still could not find root cause why test case not getting execute.
Then I found that there is one package issue in my class. So due to that I was getting compilation error in my Test class. 
Reason is quite simple, Due to compilation error class did not compile and could not create class file. 
I fixed that compilation error and executed that Test class and its run successfully.
Undercroft answered 20/9, 2022 at 6:24 Comment(0)
D
0

Are you sure your test class is in the build folder? You're invoking junit in a separate JVM (fork=true) so it's possible that working folder would change during that invocation and with build being relative, that may cause a problem.

Run ant from command line (not from Eclipse) with -verbose or -debug switch to see the detailed classpath / working dir junit is being invoked with and post the results back here if you're still can't resolve this issue.

Dibbell answered 19/4, 2011 at 12:33 Comment(0)
P
0

A variation on Guy's answer above, involving additional builders. Say you have an Ant builder configured:

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
        <arguments>
            <dictionary>
                <key>LaunchConfigHandle</key>
                <value>&lt;project&gt;/.externalToolBuilders/myprojectantlaunch.launch</value>
            </dictionary>
        </arguments>
    </buildCommand>
</buildSpec>

If the Ant build and Eclipse build have differing output locations, Eclipse may not be able to locate the test class on the classpath. In the case of an Ant builder, also check the configured targets for clean, auto, manual and after-clean builds, to ensure that the target which builds the unit tests is called.

Peppery answered 9/11, 2012 at 0:13 Comment(0)
D
0

I add this answer as my solution review from the above.

  1. You simply edit the file .project in the main project folder. Use a proper XML Editor otherwise you will get a fatal error from Eclipse that stats you can not open this project.
  2. I made my project nature Javaby adding this <nature>org.eclipse.jdt.core.javanature</nature> to <natures></natures>.
  3. I then added those lines correctly indented <buildCommand><name>org.eclipse.jdt.core.javabuilder</name><arguments></arguments></buildCommand> to <buildSpec></buildSpec>.
  4. Run as JUnit ... Success
Dearman answered 14/12, 2012 at 16:28 Comment(0)
I
0

Right click the project in the Explorer: Build Path -> Order and Export -> Select JRE System Library [jdk] and click Bottom button.

Imf answered 8/11, 2013 at 14:34 Comment(0)
H
0

Yet another variation.
Somehow, my formerly working test classes appeared to be running from some other location; my edits would not execute when I ran the tests.

I found that the output folder for my ${project_loc}src/test/java files was not what I expected. It had inadvertently been set to ${project_loc}target/classes. I set it properly in project properties, Java Build Path, Source tab.

Homiletic answered 2/5, 2014 at 2:37 Comment(0)
P
0

If you have a maven project try to run:

mvn clean compile

and then in eclipse clean & build your project.

Pathogen answered 18/6, 2014 at 0:43 Comment(0)
H
0

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.

Hesione answered 31/7, 2014 at 7:17 Comment(0)
L
0

In "Package Explorer" view, Right click your test class, then "Build Path">>"Include", it should be OK.

Longsufferance answered 26/3, 2015 at 2:43 Comment(0)
D
0

I too faced the same exception, none of the solutions over internet helped me out. my project contains multiple modules. My Junit code resides in Web module. And it's referring to client module's code.

Finally , I tried : Right click on (Web module) project -->build path--> source tab--> Link source --> added the src files location (Client module's)

Thats it! It worked like a charm Hope it helps

Dualistic answered 8/5, 2015 at 12:37 Comment(0)
L
0

I received this error because I had recently created a new workspace and my "Installed JREs" were not set up correctly. Make sure in Preferences -> Java -> Installed JREs the root folder of your JDK is selected. The default for me in a new workspace was a JRE for some reason.

Lorenelorens answered 29/10, 2015 at 19:52 Comment(0)
T
0

I know this has been already answered a long time ago but the answer didnt fix it for me but, I just removed everything from the sources in the buildpath settings and re-added them

Tiemroth answered 28/4, 2016 at 18:39 Comment(0)
R
0

I had this problem, in my case the problem relies in the compilation, I am using maven and test classes didn't compile.

I fixed it by doing a maven install (it compiles all the files), also you can check for other reasons, that avoid test to compile like if your "run configurations" is skipping the tests to save time.

Rachmaninoff answered 19/9, 2018 at 9:6 Comment(0)
O
0
  • Assumming you tried everything reasonable (delete target, refresh, clean, rebuild, maven install...) and that you realized that Eclipse does not behave reasonable...
  • Now: Check if there is no errors suggesting it can be a plugin (any plugin) that has something to do with tests.

  • In my case:

    • I removed EclEmma.
    • It started working...

Spent few hours on trying to guess it!.

Overblouse answered 4/3, 2019 at 16:51 Comment(0)
N
0

Please make sure the output folder in Java Build Path tab set as like below,which would determine where the .class file are generated.Then clean the project.

enter image description here

Neusatz answered 1/5, 2019 at 3:43 Comment(0)
M
0

The above mentioned solutions didn't work for me.

I just restarted my IDE by closing it and reopening it.

And then error disappeared and its working fine now.

Menado answered 3/2, 2020 at 10:29 Comment(0)
T
0

You may need to do a combination of the above to resolve this problem. I normally get this error when I do

mvn clean install

and see a compile error preventing the my tests from fully compiling.

Assuming the issues resolved by the above have been dealt with, try clicking Project, Clean, and clean the project which contains the test you want to run. Also make sure Build Automatically is checked.

Teletypesetter answered 2/10, 2020 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.