在Eclipse中"没有发现JUnit测试"

iaa*_*acp 47 java eclipse junit unit-testing

所以我是JUnit的新手,我们必须将它用于家庭作业.我们的教授给了我们一个有一个测试课的项目BallTest.java.当我右键单击>运行方式> JUnit Test时,我收到一个弹出错误,显示"找不到JUnit测试".我知道这个问题已在这里得到解答(测试运行'JUnit 4'没有找到测试),但关闭日食,重新启动,清理和构建似乎不起作用.下面是我的运行配置,构建路径和我正在尝试测试的类的屏幕截图.

运行配置 构建路径

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());  
}

}
Run Code Online (Sandbox Code Playgroud)

iaa*_*acp 64

右键单击Project> Properties> Java Build Path>将Test文件夹添加为源文件夹.

包括测试类在内的所有源文件夹都需要位于Eclipse Java Build Path中.这样可以将main和test类等源代码编译到build目录中(Eclipse默认文件夹是bin).

  • 如果你没有文件夹"test"作为源文件夹,eclipse就不会在那里寻找代码 (6认同)

khy*_*ylo 16

如果其他答案都不适合你,那么这对我有用.

重启eclipse

我正确配置了源文件夹,并且单元测试正确注释但仍然为一个项目获得"未找到JUnit测试".重新启动后它工作.我使用的是基于eclipse Luna 4.4.1的STS 3.6.2


Sag*_*gar 9

右键单击 - >构建路径 - >从构建路径中删除然后再次添加 - >右键单击名为"测试">"构建路径">"用作源文件夹"的文件夹.


nik*_*tix 6

我有同样的问题并解决了这样的问题:我删除了@Test注释并重新输入了它.它只是工作,我不知道为什么.


Kla*_*108 5

看来您在测试类上缺少跑步者定义,这可能是原因:

import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class BallTest {
...
}
Run Code Online (Sandbox Code Playgroud)

  • 我希望是这样,但可悲的是我仍然遇到相同的错误。 (2认同)