Class Not Found Exception when running JUnit test

前端 未结 25 635
夕颜
夕颜 2020-12-07 08:32

I am getting this error when running JUnit test in Eclipse:

Class not found com.myproject.server.MyTest
java.lang.ClassNotFoundException: com.myproject.serve         


        
相关标签:
25条回答
  • 2020-12-07 09:06

    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.

    0 讨论(0)
  • 2020-12-07 09:06

    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.
    0 讨论(0)
  • 2020-12-07 09:07

    After having tried everything here with no improvement, I solved my issue by just restarting Eclipse

    0 讨论(0)
  • 2020-12-07 09:09

    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).

    0 讨论(0)
  • 2020-12-07 09:10

    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.

    0 讨论(0)
  • 2020-12-07 09:11

    Might be you forgotten to place the Main class and Test Case class in /src/test/java. Check it Once.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题