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.
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:
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With