Java Run As JUnit 未出现在 Eclipse 中 - 使用 JUnit4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18149882/
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
Run As JUnit not appearing in Eclipse - using JUnit4
提问by rawkfist0215
I'm trying to write JUnit4 tests for my web app, and they had been working fine previously. However, now when I try to run the tests by right clicking the class file -> Run As -> JUnit Test I don't see that option. I think it could be because a colleague committed some Eclipse settings/property files on accident that messed with mine. I'm using Eclipse Helios on a Mac running 10.6.X.
我正在尝试为我的 Web 应用程序编写 JUnit4 测试,并且它们之前一直运行良好。但是,现在当我尝试通过右键单击类文件 -> 运行方式 -> JUnit 测试来运行测试时,我看不到该选项。我认为这可能是因为一位同事意外提交了一些 Eclipse 设置/属性文件,这些文件弄乱了我的。我在运行 10.6.X 的 Mac 上使用 Eclipse Helios。
I noticed that the icons on the test classes changed from the "filled" J to a "bubble" J and I'm not sure if that is signifying some kind of problem:
我注意到测试类上的图标从“填充”J 变为“气泡”J,我不确定这是否表示某种问题:
I've double checked and made sure that JUnit4 is on my Build Path, and I've gone to the Eclipse -> Preferences -> JUnit pane and verified there are JUnit4 imports being used.
我已经仔细检查并确保 JUnit4 在我的构建路径上,并且我已经转到 Eclipse -> Preferences -> JUnit 窗格并验证有正在使用的 JUnit4 导入。
My test classes look like this:
我的测试类如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "classpath*:/resources/action-test-appconfig.xml" })
@Transactional
public class UserControllerTest extends BaseStrutsTestCase<UserController> {
/**
* Tests the ability of a user to change their login username
* @throws Exception
*/
@Test
public void testChangeLogin() throws Exception {
Any thoughts and suggestions are appreciated.
任何想法和建议表示赞赏。
采纳答案by Juned Ahsan
The problem is with the way you are trying to access and run java files in eclipse. You should be observing this empty 'J' icons on your java files. It is a classpath problem, when you click, you are actually accessing the file from the classpath.
问题在于您尝试在 eclipse 中访问和运行 java 文件的方式。您应该在 Java 文件上观察这个空的“J”图标。这是一个类路径问题,当您单击时,您实际上是从类路径访问文件。
To view the Java file, you have to add a reference to your project in the classpath and move it to the top of the classpath list.
要查看 Java 文件,您必须在类路径中添加对项目的引用,并将其移至类路径列表的顶部。
Once you do that, then you should be able to run your junits.
一旦你这样做了,那么你应该能够运行你的junit。
回答by Luis Sep
That kind of J icon filled to a "bubble" means that Eclipse doesn't recognize your project as a Java project, and therefore doesn't provide Java options such as Run as JUnit.
那种填充为“气泡”的 J 图标意味着 Eclipse 不会将您的项目识别为 Java 项目,因此不提供 Java 选项,例如 Run as JUnit。
Try reimporting the project as a Java Project.
尝试将项目重新导入为 Java 项目。
回答by divyaravi
I had the same issue, and I restarted eclipse and got "Run as JUnit test" back. Looks like a bug in eclipse.
我遇到了同样的问题,我重新启动了 eclipse 并返回了“作为 JUnit 测试运行”。看起来像是eclipse中的一个错误。
回答by Andy
Try adding following dependency in the pom.xml of your project in which the test case is located:
尝试在测试用例所在项目的 pom.xml 中添加以下依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>org.springframework.test</artifactId>
<version>3.1.1.RELEASE</version>
<scope>test</scope>
</dependency>