Java Eclipse 中“未找到 JUnit 测试”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20057771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:44:59  来源:igfitidea点击:

'No JUnit tests found' in Eclipse

javaeclipseunit-testingjunit

提问by iaacp

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.

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

Run ConfigurationBuild Path

运行配置构建路径

BallTest.java

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

}

采纳答案by iaacp

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

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

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 Java 构建路径中。这样可以将 main 和 test 类等源代码编译到 build 目录中(Eclipse 默认文件夹为 bin)。

回答by Klazen108

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 {
...
}

回答by khylo

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

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

回答by nikartix

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

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

回答by Sagar

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.

右键单击-> 构建路径-> 从构建路径中删除,然后再次添加它-> 右键单击​​名为“测试”的文件夹> 构建路径> 用作源文件夹。

回答by Joshua Goldberg

The run configuration for a test class can create another cause (and solution) for this problem.

测试类的运行配置可以为这个问题创建另一个原因(和解决方案)。

If you go to Run (or Debug) Configurations (using cmd-3 or clicking on the small dropdown buttons in the toolbar) you can see a configuration created for every test class you've worked with. I found that one of my classes that wouldn't launch had a run configuration where the Test Method field had somehow gotten inadvertently populated. I had to clear that to get it to work. When cleared it shows (all methods)in light text.

如果您转到运行(或调试)配置(使用 cmd-3 或单击工具栏中的小下拉按钮),您可以看到为您使用的每个测试类创建的配置。我发现我的一个不会启动的类有一个运行配置,其中测试方法字段以某种方式被无意中填充。我必须清除它才能使其正常工作。清除后,它会以浅色文本显示(所有方法)

I'll add that strangely — maybe there was something else going on — it also seemed not to work for me until I fixed the "Name" field as well so that it included only the class name like the other JUnit run configurations.

我会奇怪地补充一点——也许还有其他事情发生——它似乎也对我不起作用,直到我修复了“名称”字段,以便它像其他 JUnit 运行配置一样只包含类名。

回答by ankit

I solved the problem by configuring the build path. Right click on the project(or any of the subfolders)-> Build path -> Configure build path. Once the property window opens up, click on the 'Source' tab and add your src and tst folders. But this alone did not work for me. Strangely, I had to retype the annotations.(Project->clean or restart might also have worked though).

我通过配置构建路径解决了这个问题。右键单击项目(或任何子文件夹)-> 构建路径-> 配置构建路径。属性窗口打开后,单击“源”选项卡并添加 src 和 tst 文件夹。但仅此一项对我不起作用。奇怪的是,我不得不重新输入注释。(不过,Project->clean 或 restart 也可能有效)。

回答by Igor Semkiv

Any solution didn't work for me until I change the name of the my test method. When name of test method starts with "test" is OK. I am new in android programing and it was for me big surprise.

在我更改测试方法的名称之前,任何解决方案都对我不起作用。当测试方法的名称以“test”开头时就可以了。我是 android 编程的新手,这对我来说是一个很大的惊喜。

回答by pintu

I think you have created your test classes outside the src folder. You can solve above problem by two way:

我认为您已经在 src 文件夹之外创建了测试类。您可以通过两种方式解决上述问题:

  1. Add your package name in java build path->source

  2. Move your package/classin srcfolder

  1. 添加你的包名 java build path->source

  2. 移动你package/classsrc文件夹

I have the same problem and solved in this way both solutions working fine.

我有同样的问题并以这种方式解决了两种解决方案都可以正常工作。

回答by Tushar Mishra

Came across this problem while upgrading projects across eclipse versions. For e.g. junits running well in Mars2.0 did not run on Neon. The following worked for me.

在跨 Eclipse 版本升级项目时遇到了这个问题。例如,在Mars2.0 中运行良好的junit 并没有在Neon 上运行。以下对我有用。

  1. Delete .settings folder. Import project into eclipse.
  2. Remove source folders. Then again use the folders as source folders. e.g - remove src/main/java from build path as source folder. -> Ok -> Again make this folder as source folder. Repeat it for main/resources, test/java, test/resources
  1. 删除 .settings 文件夹。将项目导入eclipse。
  2. 删除源文件夹。然后再次使用这些文件夹作为源文件夹。例如 - 从构建路径中删除 src/main/java 作为源文件夹。-> 好的 -> 再次将此文件夹设为源文件夹。对 main/resources、test/java、test/resources 重复它