Class Not Found Exception when running JUnit test

前端 未结 25 624
夕颜
夕颜 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:24

    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.

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

    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?"

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

    It seems compile issue. Run project as Maven test, then Run as JUnit Test.

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

    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.

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

    Making some dummy change and saving the test class can solve the problem. It will build the .class automatically

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

    In my case, only next steps helped me to resolve this issue:

    1. Project->properties->Run/Debug Settings.
    2. In "Launch configurations for '.....'" select classes/projects
    3. Edit -> Classpath -> "Restore Default Entries"
    0 讨论(0)
提交回复
热议问题