question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PowerMock+TestNG+DataProvider RuntimeException

See original GitHub issue

From 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:open
  • Created 8 years ago
  • Reactions:1
  • Comments:8

github_iconTop GitHub Comments

5reactions
johanhalebycommented, Jul 25, 2015

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); }

2reactions
korvindestcommented, Dec 7, 2017

Faced with the same issue. Are you plan to fix it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using DataProviders with Powermock+Mockito+TestNG
Hi, We are trying to use PowerMock to mock a singleton. The test has a dataprovider defined which seems to cause problems when...
Read more >
TestNG Parameterized Test and PowerMockito causes ...
TestNG Parameterized Test and PowerMockito causes MethodMatcherException ... Else Everything works fine! EDIT: Code To Reproduce the issue. build.
Read more >
CHANGES.txt - platform/external/testng - Git at Google
Fixed: expectedExceptions=RuntimeException.class was not failing when no exception was throw. Fixed: TESTNG-291: Exceptions thrown by Iterable DataProviders ...
Read more >
Java Examples for org.testng.IObjectFactory - Javatips.net
This java examples will help you to understand the usage of org.testng. ... catch (Exception e) { throw new RuntimeException("PowerMock internal error", ...
Read more >
Running TestNG unit test in Eclipse ignores latest changes-Java
Related Query · running same test with different dataprovider in testng · Run-time exception when running simple unit test with Powermock (with mockito)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found