Eclipse 中“未找到 JUnit 测试" [英] 'No JUnit tests found' in Eclipse

查看:24
本文介绍了Eclipse 中“未找到 JUnit 测试"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

}

推荐答案

右键单击项目">属性">Java 构建路径">将测试文件夹添加为源文件夹".

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

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

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

这篇关于Eclipse 中“未找到 JUnit 测试"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆