开发者

JUNIT Test class in Eclipse - java.lang.ClassNotFoundException

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.ecli开发者_高级运维pse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

any ideas on how to fix this ?

Thanks.


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.


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.


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>


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!


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.


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


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.


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!


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.


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.


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.


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.


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.


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


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


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.


If you have a maven project try to run:

mvn clean compile

and then in eclipse clean & build your project.


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.


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


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


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.


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


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.


  • 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!.


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.

JUNIT Test class in Eclipse - java.lang.ClassNotFoundException


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜