在Eclipse中没有发现JUnit测试。

时间:2022-10-15 23:40:34

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、重新启动、清理和构建似乎都不起作用。下面是我的运行配置、构建路径和我要测试的类的屏幕截图。

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

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

}

12 个解决方案

#1


52  

This fixed it:

这固定:

Right click on the folder named 'Test' > Build Path > Use as Source Folder.

右键单击“Test”>构建路径>作为源文件夹。

I don't fully understand why though, can someone comment on that?

我不太明白为什么,有人能评论一下吗?

#2


14  

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

如果其他答案对你都不起作用,下面就是对我起作用的。

Restart eclipse

重新启动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。

#3


9  

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.

右击->构建路径->从构建路径移除,然后再添加->右键单击“测试”>构建路径>作为源文件夹。

#4


4  

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

看起来您在测试类中缺少了runner定义,这可能是原因:

import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class BallTest {
...
}

#5


3  

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注释并重新键入它。它只是起作用了,我不知道为什么。

#6


2  

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程序的新手,这对我来说是一个很大的惊喜。

#7


2  

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

    在java构建路径->源代码中添加包名

  2. Move your package/class in src folder

    在src文件夹中移动包/类

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

我有同样的问题,用这种方法解决了这两个问题。

#8


2  

  1. Right click your project ->Properties->Java Build Path and go to Source Tab then add your test src folder.
  2. 右键单击您的项目->属性->Java构建路径,然后进入Source选项卡,然后添加您的test src文件夹。
  3. Select Project menu and unselect 'Build Automatically' option if selected
  4. 选择“项目”菜单,选择“自动构建”选项
  5. Select Clean and then select 'Build Automatically' option
  6. 选择Clean,然后选择“Build automatic”选项
  7. Restart Eclipse and run your junit.
  8. 重新启动Eclipse并运行您的junit。

#9


1  

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

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

#10


0  

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.

我将奇怪地添加这一内容——可能还有其他事情发生——它似乎对我也不起作用,直到我修改了“Name”字段,以便它只包含类名,就像其他JUnit运行配置一样。

#11


0  

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中运行良好的junits没有使用Neon。下面的方法对我很有效。

  1. Delete .settings folder. Import project into eclipse.
  2. 删除.settings文件夹。项目导入eclipse。
  3. 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
  4. 删除源文件夹。然后再次使用文件夹作为源文件夹。e。从构建路径中删除src/main/java作为源文件夹。-> Ok ->再次将此文件夹作为源文件夹。对主/资源、测试/java、测试/资源重复此操作

#12


0  

I was using @RunWith(Parameterized.class) but missed to add the @Parameters annotation in the public static parameters method. After adding the annotation it worked.

我正在使用@RunWith(Parameterized.class),但是没有在public static parameters方法中添加@Parameters注释。添加注释后,它工作了。

#1


52  

This fixed it:

这固定:

Right click on the folder named 'Test' > Build Path > Use as Source Folder.

右键单击“Test”>构建路径>作为源文件夹。

I don't fully understand why though, can someone comment on that?

我不太明白为什么,有人能评论一下吗?

#2


14  

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

如果其他答案对你都不起作用,下面就是对我起作用的。

Restart eclipse

重新启动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。

#3


9  

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.

右击->构建路径->从构建路径移除,然后再添加->右键单击“测试”>构建路径>作为源文件夹。

#4


4  

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

看起来您在测试类中缺少了runner定义,这可能是原因:

import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class BallTest {
...
}

#5


3  

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注释并重新键入它。它只是起作用了,我不知道为什么。

#6


2  

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程序的新手,这对我来说是一个很大的惊喜。

#7


2  

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

    在java构建路径->源代码中添加包名

  2. Move your package/class in src folder

    在src文件夹中移动包/类

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

我有同样的问题,用这种方法解决了这两个问题。

#8


2  

  1. Right click your project ->Properties->Java Build Path and go to Source Tab then add your test src folder.
  2. 右键单击您的项目->属性->Java构建路径,然后进入Source选项卡,然后添加您的test src文件夹。
  3. Select Project menu and unselect 'Build Automatically' option if selected
  4. 选择“项目”菜单,选择“自动构建”选项
  5. Select Clean and then select 'Build Automatically' option
  6. 选择Clean,然后选择“Build automatic”选项
  7. Restart Eclipse and run your junit.
  8. 重新启动Eclipse并运行您的junit。

#9


1  

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

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

#10


0  

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.

我将奇怪地添加这一内容——可能还有其他事情发生——它似乎对我也不起作用,直到我修改了“Name”字段,以便它只包含类名,就像其他JUnit运行配置一样。

#11


0  

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中运行良好的junits没有使用Neon。下面的方法对我很有效。

  1. Delete .settings folder. Import project into eclipse.
  2. 删除.settings文件夹。项目导入eclipse。
  3. 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
  4. 删除源文件夹。然后再次使用文件夹作为源文件夹。e。从构建路径中删除src/main/java作为源文件夹。-> Ok ->再次将此文件夹作为源文件夹。对主/资源、测试/java、测试/资源重复此操作

#12


0  

I was using @RunWith(Parameterized.class) but missed to add the @Parameters annotation in the public static parameters method. After adding the annotation it worked.

我正在使用@RunWith(Parameterized.class),但是没有在public static parameters方法中添加@Parameters注释。添加注释后,它工作了。