Java 手动实例化TestCase并将其添加到JUnit不会';行不通

Java 手动实例化TestCase并将其添加到JUnit不会';行不通,java,eclipse,junit,testcase,test-suite,Java,Eclipse,Junit,Testcase,Test Suite,我需要重用一个测试用例,所以我在测试用例中创建了一个私有字段“role”: public class ATest extends TestCase { private String role; public void setRole(String userName) { role = userName; } @Test public void method1(){ System.out.println("role:"+

我需要重用一个测试用例,所以我在测试用例中创建了一个私有字段“role”:

public class ATest extends TestCase {

    private String role;

    public void setRole(String userName) {
        role = userName;
    }

    @Test
    public void method1(){
        System.out.println("role:"+role);
    }
现在,我想使用相同的“ATest”类创建一个包含多个测试用例的套件:

用例1是添加测试用例的最明显的方式,但不能设置我的私有字段。案例2是我想要做的,但不起作用,因为我测试的属性fName为null(我在Eclipse中使用debug透视图看到了它)。最后一个案例3似乎有效,但我不想使用这种方法

有没有更优雅、更简单的方法来重用相同的测试用例?
谢谢

您不需要套房。您可以为此使用参数化类

@RunWith(Parameterized.class)
public class ATest { //with JUnit4, no need to extend from something
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {     
                 { "firstRole" }, { "SecondRole" }, { "ThirdRole" } 
           });
    }

    private final String role;

    public ATest(String role) {
        this.role = role;
    }

    @Test
    public void test() {
        System.out.println("role:"+role);
    }
}
@RunWith(参数化的.class)
公共类ATest{//使用JUnit4,不需要从某些内容进行扩展
@参数
公共静态收集数据(){
返回Arrays.asList(新对象[][]{
{“第一角色”}、{“第二角色”}、{“第三角色”}
});
}
私有最终字符串角色;
公共ATest(字符串角色){
this.role=角色;
}
@试验
公开无效测试(){
System.out.println(“角色:“+role”);
}
}
有关参数化测试的更多信息,请参阅

@RunWith(Parameterized.class)
public class ATest { //with JUnit4, no need to extend from something
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {     
                 { "firstRole" }, { "SecondRole" }, { "ThirdRole" } 
           });
    }

    private final String role;

    public ATest(String role) {
        this.role = role;
    }

    @Test
    public void test() {
        System.out.println("role:"+role);
    }
}