简体   繁体   English

在 Eclipse 中找不到作为 junit 测试运行

[英]can't find run as junit test in eclipse

I created a test class in Eclipse like this我在 Eclipse 中创建了一个这样的测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@TransactionConfiguration
@Transactional
public class TeamTest extends AbstractTransactionalJUnit4SpringContextTests {

 @Test
 public void testCreate() {


  assert (true);
 }}

However, when I click right click on the file I don't see option to run as JUnit!但是,当我单击右键单击该文件时,我没有看到作为 JUnit 运行的选项!

What is wrong?怎么了?

I am using Eclipse 3.6我正在使用 Eclipse 3.6

Make sure your eclipse environment is using JUnit 4. JUnit 3 doesn't make use of annotations (it uses the old extends TestCase style)确保您的 Eclipse 环境使用 JUnit 4。JUnit 3 不使用注释(它使用旧的extends TestCase样式)

There are few things to double check:有几件事需要仔细检查:

Window > Preferences > Java > JUnit

Are you seeing junit4 or junit3 imports?你看到的是junit4还是junit3进口? If that looks good, make sure the project itself is using JUnit4 instead of JUnit3.如果看起来不错,请确保项目本身使用的是 JUnit4 而不是 JUnit3。

Right Click on project > Properties > Java Build Path > Libraries

Is JUnit4 included there?是否包含 JUnit4? Is anything JUnit related there?有什么与 JUnit 相关的吗? If JUnit3 is in there, click on it and click Remove .如果 JUnit3 在那里,单击它并单击Remove Then click Add Library... and follow the prompts from there to add JUnit again.然后单击Add Library...并按照那里的提示再次添加 JUnit。

Out of curiosity, are the JUnits run outside of eclipse?出于好奇,JUnit 是否在 eclipse 之外运行? Like with a mvn install or whatever build target you have for Ant that'll run JUnits就像使用mvn install或您为 Ant 运行 JUnits 的任何构建目标一样

Write a simple test case to see if Eclipse works correctly or not.编写一个简单的测试用例来查看 Eclipse 是否正常工作。 If simple test case can be run, check your testcase, especially import classes.如果可以运行简单的测试用例,请检查您的测试用例,尤其是导入类。

Or make a try in "Run" -> "Run Configurations",fill the "Test class" as " TeamTest "(full class name).或者尝试在“运行”->“运行配置”中,将“测试类”填入“ TeamTest ”(类全名)。 Then click "Run", see what will happen...然后点击“运行”,看看会发生什么......

The way I fixed it is by changing Test runner in the Run Configuration from the default Junit3 to Junit4.我修复它的方法是将运行配置中的测试运行程序从默认的 Junit3 更改为 Junit4。 Once I made this change I could see the option Junit when I right clicked on the test class and expanded Run As进行此更改后,当我右键单击测试类并展开 Run As 时,我可以看到选项 Junit 在此处输入图片说明

I encountered the same issue and verified all the settings (above mentioned) are fine.我遇到了同样的问题并验证了所有设置(上面提到的)都很好。 A small change (version to 1.5.21.RELEASE from 2.5.0-M2) in pom.xml resolved my issue (below). pom.xml 中的一个小改动(从 2.5.0-M2 到 1.5.21.RELEASE)解决了我的问题(如下)。 This Iam able to recreate consistently.这我能够始终如一地重新创建。

STS Details Version: 3.9.0.RELEASE Build Id: 201707061823 Platform: Eclipse Oxygen (4.7.0) STS 详细信息版本:3.9.0.RELEASE Build Id:201707061823 平台:Eclipse Oxygen (4.7.0)

Pom.xml change Pom.xml 更改

FROM

<modelVersion>4.0.0</modelVersion>
<parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         **<version>2.5.0-M2</version>**
         <relativePath/> <!--  lookup parent from repository  -->
</parent>

TO

<modelVersion>4.0.0</modelVersion>
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        **<version>1.5.21.RELEASE</version>**
        <relativePath /> <!-- lookup parent from repository -->
</parent>

确保你使用 Junit 作为你的测试框架,而不是像 TestNg 这样涵盖大部分 Junit 目的的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM