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

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

我在 Eclipse 中使用菜单选项制作了一个 Junit 测试用例。 我得到了选项 "Run as JUnit Test"。 但是当我完成更改后,我注意到 "Run as JUnit Test" 消失了。

经过进一步分析,我发现我最初的 TestClass 定义如下:

public class SampleTest
{
....
}

但我不得不将 class 定义更改为以下内容

public class SampleTest<T>   
{
..
}

所以我注意到添加泛型是在创建行为。 我查看了以下链接:

Run As JUnit not appearing in Eclipse - using JUnit4

Missing "Run as JUnit Test"

public class SampleTest<T>
{
..
}

但这些链接与我的问题关系不大。 所以,我需要了解包含泛型与行为有关的原因。

我可能无法补充为什么泛型会从开始删除 JUnit,但通常测试 classes 充当包装器来测试特定的 class,例如:

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 {

        ...
    }
}

对于 运行 JUnit 测试 class Eclipse 需要

  1. 通过调用其默认构造函数创建测试实例class,
  2. 对该实例调用所有 @Test 方法

问题出在第 1 步:Eclipse 不知道在构造函数中使用哪种类型T
它应该使用 new SampleTest<Object>()new SampleTest<WhatEver>()?
所以它决定它不是一个有效的 JUnit 测试 class 并且不提供 运行 作为 JUnit 测试 选项。