简体   繁体   English

创建 Junit 测试用例后,“Run as Junit Test”消失

[英]After creation of Junit test cases, the "Run as Junit Test" disappears

I had made a Junit Test case using menu options in Eclipse.我使用 Eclipse 中的菜单选项制作了一个 Junit 测试用例。 I was getting the option "Run as JUnit Test".我得到了“作为 JUnit 测试运行”的选项。 But when I was done with my changes, I noticed that the "Run as JUnit Test" disappeared.但是当我完成更改后,我注意到“Run as JUnit Test”消失了。

On further analysis I found that my initially my TestClass definition was as below :在进一步分析中,我发现我最初的 TestClass 定义如下:

public class SampleTest
{
....
}

But I had to change the class definition to follows但我不得不改变类定义如下

public class SampleTest<T>   
{
..
}

So what I noticed was adding the generic was creating the behavior.所以我注意到添加泛型正在创建行为。 I looked at the following links:我查看了以下链接:

Run As JUnit not appearing in Eclipse - using JUnit4 Run As JUnit 未出现在 Eclipse 中 - 使用 JUnit4

Missing "Run as JUnit Test" 缺少“作为 JUnit 测试运行”

public class SampleTest<T>
{
..
}

But these links are less related to my issue.但这些链接与我的问题关系不大。 So, I need to understand the reason what including Generics has to do with the behavior.所以,我需要了解包括泛型与行为有关的原因。

For running a JUnit test class Eclipse needs to为了运行 JUnit 测试类,Eclipse 需要

  1. create an instance of your test class by calling its default constructor,通过调用其默认构造函数来创建测试类的实例,
  2. call all the @Test methods on that instance调用该实例上的所有@Test方法

Here the problem is in step 1: Eclipse doesn't know which type T to use in the constructor.问题出在第 1 步:Eclipse 不知道在构造函数中使用哪种类型T
Should it use new SampleTest<Object>() or new SampleTest<WhatEver>() ?它应该使用new SampleTest<Object>()还是new SampleTest<WhatEver>()
So it decides that it is not a valid JUnit test class and doesn't offer the Run as JUnit Test option.所以它决定它不是一个有效的 JUnit 测试类,并且不提供Run as JUnit Test选项。

I may not be able to add much as to why Generics would remove JUnit from starting, but usually test classes act as a wrapper to test a specific class, such as:我可能无法解释为什么泛型会从一开始就删除 JUnit,但通常测试类充当包装器来测试特定类,例如:

public class Sample<T>
{
    ...
}

public class SampleTest
{

    Sample<TypeHere> sample;

    @Before
    public void setUp() throws Exception {
    }
        sample= new Sample<OrTypeHere>(...);
        ...
    }

    @Test
    public void whenConditionOne_verifyExpectedSituation() throws Exception {

        ...
    }
}

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

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