JUNIT Test class in Eclipse - java.lang.ClassNotFoundException

后端 未结 26 1928
栀梦
栀梦 2020-12-14 13:55

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 requir

相关标签:
26条回答
  • 2020-12-14 14:28

    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 讨论(0)
  • 2020-12-14 14:30

    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!

    0 讨论(0)
  • 2020-12-14 14:30

    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
    0 讨论(0)
  • 2020-12-14 14:30
    • 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!.

    0 讨论(0)
  • 2020-12-14 14:31

    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.

    0 讨论(0)
  • 2020-12-14 14:35

    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.

    0 讨论(0)
提交回复
热议问题