vijayjcp / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 2 forks source link

PowerMock+TestNG+DataProvider RuntimeException #464

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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.bookingForSeparateDayShouldRemain
ClassroomAvailable(lt.ignas.classroombooking.HourOfWeek,lt.ignas.classroombookin
g.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(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 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 reported on code.google.com by Ignas.Tr...@gmail.com on 1 Oct 2013 at 1:01

GoogleCodeExporter commented 8 years ago
Hi, even am facing this issue with testng-6.8.7 and powermock-mockito-1.5.3.

I tried to pass a custom class through data provider and the test is failing 
with a run time exception (Method invocation error).

Also, i tried to use the alternative option mentioned in AppendixB and then it 
is executing the test method but when i tried to cast the 'Object' to the 
original type is giving a ClassCastException. I think is related to some class 
loader issue.

 @Test (dataProvider = "getWeekday")
    public void forAllWeekdaysAllClassroomsShouldBeAvailableInitially(Object p) {
        CustomClass obj = (CustomClass)p
        assertTrue(true);
    }

Could any one please help me on this. This is a very important feature we 
required in our project to write the unit test cases and is blocking us to use 
Power Mock

Original comment by majetyya...@gmail.com on 23 Jan 2014 at 12:24

GoogleCodeExporter commented 8 years ago
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);
}

Original comment by verma....@gmail.com on 4 Sep 2014 at 4:10