'No JUnit tests found' in Eclipse

136,593

Solution 1

Right Click on Project > Properties > Java Build Path > Add the Test folder as source folder.

All source folders including Test Classes need to be in Eclipse Java Build Path. So that the sources such as main and test classes can be compiled into the build directory (Eclipse default folder is bin).

Solution 2

If none of the other answers work for you, here's what worked for me.

Restart eclipse

I had source folder configured correctly, and unit tests correctly annotated but was still getting "No JUnit tests found", for one project. After a restart it worked. I was using STS 3.6.2 based of eclipse Luna 4.4.1

Solution 3

right click -> build path -> remove from build path and then again add it -> Right click on the folder named 'Test' > Build Path > Use as Source Folder.

Solution 4

I had the same problem and solved like this: I deleted @Test annotation and retyped it. It just worked, I have no idea why.

Solution 5

It looks like you're missing the runner definition on your test class, that could be the cause:

import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class BallTest {
...
}
Share:
136,593
iaacp
Author by

iaacp

Updated on July 16, 2021

Comments

  • iaacp
    iaacp almost 3 years

    So I'm new to JUnit, and we have to use it for a homework assignment. Our professor gave us a project that has one test class, BallTest.java. When I right click > Run as > JUnit Test, I get a popup error that says 'No JUnit tests found'. I know the question has been answered here(No tests found with test runner 'JUnit 4'), but closing eclipse, restarting, cleaning, and building doesn't seem to work. Below are screenshots of my run configuration, build path, and the class I'm trying to test.

    Run Configuration Build Path

    BallTest.java

    import static org.junit.Assert.*;
    import junit.framework.Assert;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.JUnitCore; 
    import org.junit.runner.Result; 
    import org.junit.runner.notification.Failure; 
    
    public class BallTest {
    
    Ball ball;
    
    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
        System.out.println("Setting up ...");
        Point2D p = new Point2D(0,0);
        ball = new Ball(p);
    }
    
    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
        System.out.println("Tearing down ...");
        ball = null;
    }
    
    /**
     * Test method for {@link Ball#getCoordinates()}.
     */
    @Test
    public void testGetCoordinates() {
        assertNotNull(ball); // don't need Assert. because of the import statement above.
        Assert.assertEquals(ball.getCoordinates().getX(), 0);
        Assert.assertEquals(ball.getCoordinates().getY(), 0);
    }
    
    /**
     * Test method for {@link Ball#setCoordinates(Point2D)}.
     */
    @Test
    public void testSetCoordinates() {
        Assert.assertNotNull(ball);
        Point2D p = new Point2D(99,99);
        ball.setCoordinates(p);
        Assert.assertEquals(ball.getCoordinates().getX(), 99);
        Assert.assertEquals(ball.getCoordinates().getY(), 99);
    }
    
    /**
     * Test method for {@link Ball#Ball(Point2D)}.
     */
    @Test
    public void testBall() {
        Point2D p = new Point2D(49,30);
        ball = new Ball(p);
        Assert.assertNotNull(ball);
        Assert.assertEquals(ball.getCoordinates().getX(), 49);
        Assert.assertEquals(ball.getCoordinates().getY(), 30);
    
        //fail("Not yet implemented");
    }
    
    public static void main (String[] args) {
             Result result = JUnitCore.runClasses(BallTest.class);
             for (Failure failure : result.getFailures()) { 
                    System.out.println(failure.toString()); 
                } 
            System.out.println(result.wasSuccessful());  
    }
    
    }
    
  • dkatzel
    dkatzel over 10 years
    I did not know about the Juni4 runner alias. I've always explicitly mentioned the runner class name.
  • Klazen108
    Klazen108 over 10 years
    Are you referring to the package prefix import for JUnit4? If so, I know what you mean, Eclipse won't auto-import the package for you if the only reference to JUnit4 is in the annotation. It gets old quickly if you have a lot of test classes like I did :)
  • iaacp
    iaacp over 10 years
    I was hoping this would be it, but sadly I'm still getting the same error.
  • Klazen108
    Klazen108 over 10 years
    When you run the test as an application (the main method) does it execute the tests? If so, it's an eclipse configuration issue. Otherwise it's the code somehow. Let us know if that works.
  • iberbeu
    iberbeu over 10 years
    If you don't have the folder "test" as source folder, eclipse is not going to look for code there
  • Arunchunaivendan
    Arunchunaivendan over 8 years
    1. Under src folder, main and test folders should be present by default. If not, create a test folder then write the test cases/suites. 2. After creating cases/suites go to Debug or Run configurations and add JUnit test case under Test menu (If you don't find one, add JUnit to your build path). 3. Then as khylo suggested, restart eclipse. Happy testing.
  • Rajith Gun Hewage
    Rajith Gun Hewage over 7 years
    You might want to try restarting eclipse if this doesn't fix it, as @khylo 's answer says.
  • NIKHIL CHAURASIA
    NIKHIL CHAURASIA over 7 years
    Appreciation for this crazy solution, :D that works like charm, hehehe
  • LConrad
    LConrad about 6 years
    I had a similar issue - somehow my test had gotten added to the "Excluded:" area on the Source tab of Java Build Path.
  • Rasshu
    Rasshu over 5 years
    That screwed my Build Path.
  • Paul Marcelin Bejan
    Paul Marcelin Bejan over 3 years
    You're a genius! Thanks!