score:140

Accepted answer

this appears to occur because only the source code is compiling when you use mvn clean compile (i'm using maven 3.1.0 so i'm not sure if it always behaved like this).

if you run mvn test, the test code will compile as well, but then it runs the tests (which may not be immediately desirable if you're trying to run them through eclipse.) the way around this is to add test-compile to your maven command sequence whenever you do a mvn clean. for example, you would run mvn clean compile test-compile.

score:-6

might be you forgotten to place the main class and test case class in /src/test/java. check it once.

score:-1

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.

score:-1

i had faced the same issue. i solved it by removing the external junit jar dependency which i added by download from the internet externally. but then i went to project->properties->build path->add library->junit->choosed the version(ex junit4)->apply.

it automatically added the dependency. it solved my issue.

score:-1

making some dummy change and saving the test class can solve the problem. it will build the .class automatically

score:0

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.

score:0

1- mvn eclipse:eclipse

2- project clean all projects

3- restart

score:0

it's worth mentioning as another answer that if you're using egit, and your classpath gets updated because of say, a test coverage tool like clover, that sometimes there's a cleanup hiccup that does not completely delete the contents of /path/to/git/repository/<project name>/bin/

essentially, i used eclipse's error log view, identified what was causing issues during this cleanup effort, navigated to the source directory, and manually deleted the <project name>/bin directory. once that finished i went back to eclipse and refreshed (f5) my project and the error went away.

score:0

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

score:0

i had the same problem with a gradle project with a test sourceset with two resource directories.

this snippet comes from a main-module.gradle and adds a resource dir to the test sourceset:

sourcesets {
    test {
        resources {
            srcdir('../other-module/src/test/resources')
        }
    }
}

doing this i had two resource directories related to the test sourceset of the project main-module:

../other-module/src/test/resources src/test/resources (relative to the main-module folder, automatically added by the java plugin)

i find out that if i had two files with the same name in both the source directories, something in the process resources stage went wrong. as result, no compilation started and for this reason no .class were copied in the bin directory, where junit was looking for the classes. the classnotfoundexception disappeared just renaming one of the two files.

score:0

it seems compile issue. run project as maven test, then run as junit test.

score:0

for project that does not use maven : this worked for me https://ihategeek.wordpress.com/2012/04/18/eclipse-junit-test-class-not-found/

adding the jre and project src at the bottom in order and exports in build path

score:0

pls check if you have added junit4 as dependency.

e.g

<dependency>
    <groupid>junit</groupid>
    <artifactid>junit</artifactid>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

score:0

for me i had to put the project x/src/test/java/ at the bottom of the "order and export" in the "java build path"

enter image description here

score:1

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.

score:1

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.

score:1

noclassdeffounderror really means it can't initilize the class. it has nothing to do with finding the class. i got this error when calling trim() on a null string.

junit won't show nullpointerexception. the string isn't null when running normally because i'm fetching the string from a properties file which is not availible for tests.

my advice is to remove pieces from the class until your tests start passing. then you can determine which line is giving the error.

score:1

in my case, changing the order of maven dependencies from build path configuration did not work for me. i changed its order from run configuration.
follow these steps:
• go to run -> run configurations...
• click on the unit test's run configuration and click on the classpath tab.
• check use temporary jar to specify classpath (to avoid classpath length limitations).
may be it is enough and your test works. you should try, so press run button. if it did not work. follow all previous steps again and without pressing run button, go to the next step.
• in sub branches of user entries move maven dependencies above your project or test class.
• click run button.

score:3

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"

score:4

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.

score:8

i fixed my issue by running maven update. right click project your project > maven > update project

score:11

after having tried everything here with no improvement, i solved my issue by just restarting eclipse

score:18

if this problem occurs in eclipse only, executing command project -> clean... on selected project may help.

score:31

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

score:118

i had a similar problem with my tests and found somewhere in the web, that you have to go to build path in your project's properties and move maven dependencies above jre system library. that seems to have worked for me.


Related Query

More Query from same tag