简体   繁体   English

在带有JUnit的参数化测试类中使用枚举成员时出现异常

[英]Exception when using an enum member in parameterized test class with JUnit

I have a parameterized test class with an enum member as parameter. 我有一个参数化的测试类,枚举成员作为参数。

public enum MyEnum {
    A,
    B
}

This is the significant part of the test class: 这是测试类的重要部分:

@ParameterizedRobolectricTestRunner.Parameters
public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {
            {MyEnum.A}
    });
}

public MyTestClass(MyEnum value) {
}

When running the tests, I get this exception: 运行测试时,我得到以下异常:

java.lang.IllegalArgumentException: argument type mismatch

If I change the constructor to 如果我将构造函数更改为

public MyTestClass(Object value) {
    MyEnum x = (MyEnum)value;
}

I get this exception: 我得到这个例外:

java.lang.ClassCastException: com.test.MyEnum cannot be cast to com.test.MyEnum

Can anybody tell me whats going on there? 有人能告诉我那里有什么事吗? Especially the second case seems totally strange. 特别是第二种情况似乎很奇怪。 I'm mainly a C# developer, so maybe this is special case in Java? 我主要是一个C#开发人员,所以也许这是Java的特例? If I use other data types like Integer it works fine. 如果我使用像Integer这样的其他数据类型,它可以正常工作。

Thanks for helping! 谢谢你的帮助!

Edit: The enum has actually 8 members, I just changed it here. 编辑:枚举实际上有8个成员,我刚才在这里改了。 Also, the constructor has more than one parameter, I just simplified the example. 此外,构造函数有多个参数,我只是简化了示例。 The type of value is correctly com.test.MyEnum 值的类型是com.test.MyEnum

Edit2: The ParameterizedRobolectricTestRunner is the problem. Edit2:ParameterizedRobolectricTestRunner是问题所在。 If I use the (standard) Parameterized TestRunner, everything works fine. 如果我使用(标准)参数化TestRunner,一切正常。 In this special case it is ok, since I don't test UI. 在这种特殊情况下它是可以的,因为我不测试UI。 But when testing UI, the problem would still occur. 但是在测试UI时,问题仍然会发生。

That Class cast exception is quite strange. 该类强制转换异常非常奇怪。 However, the following code ran for me: 但是,以下代码为我运行:

@RunWith(Parameterized.class)
public class ParamTest {

  MyEnum expected;

  public enum MyEnum{A,B}

  // Each parameter should be placed as an argument here
  // Every time runner triggers, it will pass the arguments
  public ParamTest(MyEnum expected) {
    this.expected = expected;
  }

  @Parameterized.Parameters
  public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {
        { MyEnum.A },
        { MyEnum.B },
    });
  }

  // This test will run 2 times
  @Test
  public void myTest() {
    System.out.println("Enum is : " + expected);
    assertEquals(expected, expected);
  }
}

It prints: 它打印:

Enum is : A Enum是:A
Enum is : B Enum是:B

You can also do it like below. 您也可以像下面这样做。 It's a little bit cleaner. 它有点清洁。

@RunWith(Parameterized.class)
public class ParamTest {

  public enum MyEnum{A,B}

  @Parameterized.Parameter public MyEnum expected;

  @Parameterized.Parameters
  public static MyEnum[] data() {
    return MyEnum.values();
  }

  @Test
  public void myTest() {
    System.out.println("Enum is : " + expected);
    assertEquals(expected, expected);
  }
}

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

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