Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? If I use other data types like Integer it works fine.

Thanks for helping!

Edit: The enum has actually 8 members, I just changed it here. Also, the constructor has more than one parameter, I just simplified the example. The type of value is correctly com.test.MyEnum

Edit2: The ParameterizedRobolectricTestRunner is the problem. If I use the (standard) Parameterized TestRunner, everything works fine. In this special case it is ok, since I don't test UI. But when testing UI, the problem would still occur.

like image 643
Jan-Patrick Ahnen Avatar asked Jul 30 '14 16:07

Jan-Patrick Ahnen


People also ask

How to write parameterized test with expected exceptions in Junit4?

In JUnit4 you can write parameterized unit tests by providing parameters collection in one method, which will be passed to the constructor of the test and testing in another method. If I have a parameter for which I expect an exception to be thrown, how do I specify that? this is how i use junit parameterized test with expected exceptions:

Why is my JUnit test throwing an exception?

This is because you are expecting an exception from the method you are Unit Testing, otherwise our JUnit test would fail. Example@Test (expected=IllegalArgumentException.class) By using “expected” parameter, you can specify the exception name our test may throw.

How to run a test with values from enumeration @enumsource?

To run a test with values from enumeration @EnumSource provides a convenient way to use Enum constants. The annotation’s value attribute is optional. When omitted, the declared type of the first method parameter is used. The annotation provides an optional names attribute that lets you specify which constants shall be used to test.

How to do exception testing in Java?

Let's understand exception testing by creating a Java class with a method throwing an exception. You will handle it and test it in a test class. Consider JUnitMessage.java having a method which simply do a mathematical operation based on input received by the user.


1 Answers

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 is : B

like image 194
nmore Avatar answered Oct 17 '22 03:10

nmore