PowerMock+TestNG+DataProvider RuntimeException
See original GitHub issueFrom Ignas.Tr…@gmail.com on October 01, 2013 15:01:03
What steps will reproduce the problem? ======================================
@Test @PrepareForTest public class StaticTest extends PowerMockTestCase {
@DataProvider
public final Object[][] getWeekday() {
return new Object[][] {
{new CustomClass()}
};
}
@Test (dataProvider = "getWeekday")
public void forAllWeekdaysAllClassroomsShouldBeAvailableInitially(CustomClass p) {
assertTrue(true);
}
}
class CustomClass {} What is the expected output? What do you see instead? ===================================================== I see: java.lang.RuntimeException: Can’t invoke method public void lt.ignas.classroombooking.ReservationPlaceTest.bookingForSeparateDayShouldRemainClassroomAvailable(lt.ignas.classroombooking.HourOfWeek,lt.ignas.classroombooking.HourOfWeek), probably due to classloader mismatch
I expect: test to start with dataProvider provided object and to pass succesfully. What version of the product are you using? On what operating system? ==================================================================== UBUNTU 12.04
java version “1.7.0_40” Java™ SE Runtime Environment (build 1.7.0_40-b43) Java HotSpot™ 64-Bit Server VM (build 24.0-b56, mixed mode)
<dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.7</version> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-testng</artifactId> <version>1.5.1</version> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.5.1</version> <exclusions> <exclusion> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> </exclusion> </exclusions> </dependency> Please provide any additional information below. ================================================ 1. Without '@PrepareForTest' test PASSES. 2. Without 'extends PowerMockTestCase' test PASSES. 3. If DataProvider gives STD ( http://en.wikipedia.org/wiki/Standard_library ) class object (like StringBuilder, Object, int, String, etc.) (and test method requires the same type) test PASSES (see APPENDIX A below) 4. If DataProvider gives any custom class included in my project (like CustomClass in the example above) (and test metho requires that type) - test FAILS. 5. If DataProvider gives any custom class included in my project (like CustomClass in the example above) BUT test method requires Object - test PASSES. (see APPENDIX B below)Last statement is pretty good workround, but Java’s compile time checkings goes away. And explicit cast is needed.
===== APPENDIX A ======
@Test @PrepareForTest public class StaticTest extends PowerMockTestCase {
@DataProvider
public final Object[][] getWeekday() {
return new Object[][] {
{new StringBuilder()}
};
}
@Test (dataProvider = "getWeekday")
public void forAllWeekdaysAllClassroomsShouldBeAvailableInitially(StringBuilder p) {
assertTrue(true);
}
}
===== APPENDIX B ======
@Test @PrepareForTest public class StaticTest extends PowerMockTestCase {
@DataProvider
public final Object[][] getWeekday() {
return new Object[][] {
{new CustomClass()}
};
}
@Test (dataProvider = "getWeekday")
public void forAllWeekdaysAllClassroomsShouldBeAvailableInitially(Object p) {
assertTrue(true);
}
}
class CustomClass {}
_Original issue: http://code.google.com/p/powermock/issues/detail?id=464_
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:8
From verma…@gmail.com on September 03, 2014 21:10:49
AFAIK if you are using any PowerMock:PrepareForTest, you should not use the custom class as test case input.
You can always put it like public void forAllWeekdaysAllClassroomsShouldBeAvailableInitially(Object p) { customClass = (CustomClass) p; assertTrue(true); }
Faced with the same issue. Are you plan to fix it?