TestNG参数化测试和PowerMockito导致MethodMatcherException

问题描述 投票:0回答:1

我正在使用

org.powermock:powermock-module-testng:1.7.3
org.powermock:powermock-api-mockito:1.7.3
org.testng:testng:6.11

在我添加之前,一切正常

org.powermock.core.classloader.annotations.PrepareForTest注释并使用org.powermock.modules.testng.PowerMockTestCase扩展该类,如enter image description here中所示

它会抛出错误

org.testng.internal.reflect.MethodMatcherException: 
Data provider mismatch
Method: myParameterizedTest([Parameter{index=0, type=javax.servlet.http.HttpServletRequest, declaredAnnotations=[]}, Parameter{index=1, type=javax.servlet.http.HttpServletResponse, declaredAnnotations=[]}, Parameter{index=2, type=boolean, declaredAnnotations=[]}, Parameter{index=3, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=4, type=java.lang.String, declaredAnnotations=[]}, Parameter{index=5, type=java.lang.Integer, declaredAnnotations=[]}, Parameter{index=6, type=java.lang.Integer, declaredAnnotations=[]}, Parameter{index=7, type=org.springframework.http.HttpStatus, declaredAnnotations=[]}, Parameter{index=8, type=java.lang.Integer, declaredAnnotations=[]}])
Arguments: [($javax.servlet.http.HttpServletRequest$$EnhancerByMockitoWithCGLIB$$471f26c6)Mock for HttpServletRequest, hashCode: 1677543539,($javax.servlet.http.HttpServletResponse$$EnhancerByMockitoWithCGLIB$$5ed2ff98)Mock for HttpServletResponse, hashCode: 504478805,(java.lang.Boolean)false,(java.lang.String)2018-02-13,(java.lang.String)2018-02-28,(java.lang.Integer)1,(java.lang.Integer)10,(org.springframework.http.HttpStatus)200,(java.lang.Integer)1]

    at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:49)
    at org.testng.internal.Invoker.injectParameters(Invoker.java:1293)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1187)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

其他一切正常!

编辑:

代码重现问题

的build.gradle

group 'parameterized-test'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'org.testng', name: 'testng', version: '6.14.2'
    testCompile group: 'org.powermock', name: 'powermock-module-testng', version: '1.7.3'
    testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.7.3'
}

name UT IL.Java

package com.practice.two;

public class NameUtil {
    public static String capMe(String string){
        return string.toUpperCase();
    }
}

simple class.Java

package com.practice.two;

public class SimpleClass {
    private String name;

    public SimpleClass(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

main test.Java

package com.practice.two;

import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@PrepareForTest(NameUtil.class)
public class MainTest extends PowerMockTestCase {

    @BeforeMethod
    public void setUp() {
        PowerMockito.mockStatic(NameUtil.class);
    }

    @DataProvider(name = "demoDataProvider")
    public Object[][] demoDataProvider() {
        return new Object[][]{
                {new SimpleClass("Yudhisthir"), "Yudhisthir", 24},
                {new SimpleClass("Bheem"), "Bheem", 22},
                {new SimpleClass("Arjun"), "Arjun", 26},
                {new SimpleClass("Nakul"), "Nakul", 21},
                {new SimpleClass("Sahdev"), "Sahdev", 23}
        };
    }

    @Test(dataProvider = "demoDataProvider")
    public void testMethod(SimpleClass sc, String name, Integer integer) {
        Mockito.when(NameUtil.capMe(Mockito.anyString())).thenReturn("MAHABAHRAT");
        System.out.println(NameUtil.capMe(name));
        System.out.println(integer);
        System.out.println(sc);
    }
}
java testng powermock testng-dataprovider
1个回答
1
投票

你遇到过这个bug - https://github.com/powermock/powermock/issues/484

而你也将面对这一个-https://github.com/powermock/powermock/issues/678

以下代码按预期工作:

的build.gradle

dependencies {
        testCompile group: 'org.testng', name: 'testng', version: '6.9.13.6'
        testCompile group: 'org.powermock', name: 'powermock-module-testng', version: '2.0.0-beta.5'
        testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.0-beta.5'
}

main test.Java

import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


@PrepareForTest(NameUtil.class)
public class MainTest extends PowerMockTestCase {

    @BeforeMethod
    public void setUp() {
        PowerMockito.mockStatic(NameUtil.class);
    }

    @DataProvider(name = "demoDataProvider")
    public Object[][] demoDataProvider() {
        return new Object[][]{
                {new methodMatcherException.MainTest.SimpleClass("Yudhisthir"), "Yudhisthir", 24},
                {new methodMatcherException.MainTest.SimpleClass("Bheem"), "Bheem", 22},
                {new methodMatcherException.MainTest.SimpleClass("Arjun"), "Arjun", 26},
                {new methodMatcherException.MainTest.SimpleClass("Nakul"), "Nakul", 21},
                {new methodMatcherException.MainTest.SimpleClass("Sahdev"), "Sahdev", 23}
        };
    }

    @Test(dataProvider = "demoDataProvider")
    public void testMethod(Object o, String name, Integer integer) {
        SimpleClass sc = (SimpleClass) o;
        Mockito.when(NameUtil.capMe(Mockito.anyString())).thenReturn("MAHABAHRAT");
        System.out.println(NameUtil.capMe(name));
        System.out.println(integer);
        System.out.println(sc);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.