+1  A: 

your build classpath is correct, which is why you can compile. the classpath for your JUnit needs to be checked. go to the Run menu and choose 'open run dialog.' in there you should see a tree on the left with JUnit as an option. open that node and find and select your test. on the right pane you will see a tab for classpath. take a look to ensure that your class that the test is trying to instantiate would be found.

edit:

this seems to be an issue with maven and its behavior after a release changed the default Eclipse output folders. i have seen solutions described where

  • placing maven into the bootclasspath ABOVE the jre works, or
  • running mvn clean test does the trick or
  • refreshing all of your eclipse projects, causing a rebuild fixes the problem
  • going to your project and selecting Maven->Update Configuration solve the problem

with the first three, there were reports of the issue recurring. the last looks best to me, but if it doesnt work, please try the others.

here and here is some info

akf
unfortunately, "Maven => Update Configuration" does not solve the problem at all... the reverse is true; it delete's the build path configurations...Or does that mean that some other configuration is bad?
swalkner
A: 

Have you tried right clicking on your project root, selecting "properties", and making sure that the CLASSPATH is correct? If I recall correctly, that's how you do it.

Anything about the way Eclipse runs unit tests that requires you to add the junit JAR to the runtime CLASSPATH in a special way?

I use IntelliJ, so I don't have these issues.

I'd check Eclipse myself, but I prefer not having it on my desktop.

duffymo
okay, without leading to a Eclipse vs. IntelliJ-discussion; I'd like to have it in Eclipse running... and I checked the Java Build Path - the Java JDK is there, and I don't need anything else for my HelloWorld example...I think it's gotta something to do with 'source folders on build path'... I set them to the folder where my package starts... I assume that's correct.
swalkner
You do need something besides the JDK - that's the path to HelloWorld.class, right? "source folders on build path" - sounds better. No need to assume, be an experimentalist and try it. Eclipse will tell you if you're correct.
duffymo
A: 

Hmm, looks a little bizarre, try running it with the following annotation at the top of the class:

@RunWith(SpringJUnit4ClassRunner.class)
public class UserDaoTest {
}

and let me know how you get on with it.

Check that you have build automatically enabled as well. If you want to make sure your test classes are being compiled correctly clear out the Maven target folder (and any bin folder that Eclipse may be using). Are you using m2eclipse as well, as I find it to be a little problematic.

Jon
thanks a lot for your answer, but nevertheless: it doesn't work either; first, I have to include spring-test.jar, then another dependency is not fullfilled.i don't understand why it doesn't work with a simple helloworld-class in the same package. and there, i don't need a "hack" like the one suggested by you... something must be wrong with "classpath" or something similar, but i don't get where/what i have to check...
swalkner
A: 

Hi guys.

I've come across that situation several times and, after a lot of attempts, I found the solution.

Check your project build-path and enable specific output folders for each folder. Go one by one though each source-folder of your project and set the output folder that maven would use.

For example, your web project's src/main/java should have target/classes under the web project, test classes should have target/test-classes also under the web project and so.

Using this configuration will allow you to execute unit tests in eclipse.

Just one more advice, if your web project's tests require some configuration files that are under the resources, be sure to include that folder as a source folder and to make the proper build-path configuration.

Hope it helps.

Carlos

Carlos
Hi Carlos,thanks a lot for your advice.But it didn't help, unfortunately.I added /src/main/java (target/classes) and /src/test/java (target/test-classes) to the source folders as well as src/main/resources (applicationContext.xml, some properties - target/classes).Same problem - when starting my src/test/java/UserDaoTest.java as JUnit-test from Eclipse, I get "Class not found UserDaoTestjava.lang.ClassNotFoundException: UserDaoTest". At least, that's a little bit different then the original error message...
swalkner
You're on the right way, check whether the UserDaoTest class is in the corresponding output folder within your file system (As you may know, Eclipse does compile all your source files and place it into the specified output folder, among others, to be able to launch tests). If the class doesn't appear in the output folder, check the inclusion/exclusion filters of the build path for this specific folder. Good luck.
Carlos
+2  A: 

This was my solution to the problem. Of course, many things can cause it to occur. For me it was that Maven2 (not the plugin for Eclipse) was setting the eclipse profile up to use a different builder (aspectJ) but I did not have the plugin in eclipse./

http://rbtech.blogspot.com/2009/09/eclipse-galileo-javalangclassnotfoundex.html

Cheers Ramon Buckland

A: 

Hi,

Its very Old Jul (which year) but I had the same problem .

Actual issue found that eclipse was not able to generate class file for the java file , classpath was proper.

See the problem tab and check if your project is missing something/file. you can create a new proj and add files one by one and build them until it stops compiling and creating classes ( check the workspace/proj/bin/package/ folder for classes )

its wierd but true , ecplise was failing in compliation because 4 of 20 java files were using a single image which was missing. and as result none of the java file was compiled .

CLASSPATH is not a issue here.

Sachin
A: 

The solution to my problem which was similar: the libs were invalid. If you look in the .classpath file of the project, you'll see classpathentry tags with the key/value kind="lib". Some of mine were incorrect.

I didn't discover this until I turned off Validation settings. That is, there were so many errors in the JSP files, etc, that the classpath errors weren't evident (or possibly even showing up). As a result, nothing was being compiled into the destination output folders, but no helpful errors on why.

Mike