1

我正在使用黄瓜 BDD、testng、java 来编写一些 BDD 测试。我想模拟静态类以编写我的测试。但是,当我编写此测试运行程序时,它无法初始化 BDD 场景。

完整示例(注意注释行PrepareForTest):

import gherkin.events.PickleEvent;
import io.cucumber.testng.CucumberOptions;
import io.cucumber.testng.PickleEventWrapper;
import io.cucumber.testng.TestNGCucumberRunner;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import static org.mockito.Matchers.any;

@CucumberOptions(
        features = {
                "src/test/resources/features/sample"
        },
        glue = {
                "com.demo.stepdefinitions.sample"
        },
        plugin = {
                "pretty",
                "html:target/cucumber-reports/cucumber-pretty",
                "json:target/cucumber-reports/sampple-report.json",
                "rerun:target/cucumber-reports/sample-rerun.txt"
        }
)
//@PrepareForTest({Util.class})
public class TestngWithDataproviderTest extends PowerMockTestCase {
    private TestNGCucumberRunner testNGCucumberRunner;
    private void mockActiveBucket() {
        PowerMockito.mockStatic(Util.class);
        PowerMockito.when(Util.getBucketId(any(Long.class))).thenReturn(3);
    }

    @BeforeClass(alwaysRun = true)
    public void setUpClass() throws Exception {
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }

    @Test(dataProvider = "users")
    public void testMockStatic(String username){
        System.out.println("username: " + username);
        System.out.println("static test passed");
        mockActiveBucket();
        Assert.assertTrue(true);
    }

    @Test(groups = "cucumber scenarios", description = "Runs Cucumber Scenarios", dataProvider = "scenarios")
    public void testCucumberCcenario(PickleEventWrapper pickleEvent) throws Throwable {
        PickleEvent event = pickleEvent.getPickleEvent();
        mockActiveBucket();
        testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());
        Assert.assertTrue(true);
    }

    @DataProvider(name = "scenarios")
    public Object[][] scenarios() {
        Object[][] scenarios = testNGCucumberRunner.provideScenarios();
        return new Object[][]{{scenarios[0][0]}};
    }

    @DataProvider(name = "users")
    public Object[][] users() {
        return new Object[][]{{"user1"}, {"user2"}};
    }
}

class Util {
    public static int getBucketId(long eventTimestamp){
        Long minsPast5MinBoundary = (eventTimestamp % TimeUnit.MINUTES.toMillis(5))/TimeUnit.MINUTES.toMillis(1);
        return minsPast5MinBoundary.intValue();
    }
}

如果我在测试中启用注释,则上述测试无法加载 BDD 场景dataProvider 。PrepareForTest但是,使用dataProvider的其他测试在两种情况下都可以正常工作(启用或禁用PrepareForTest

错误:

Data provider mismatch
Method: testCucumberCcenario([Parameter{index=0, type=io.cucumber.testng.PickleEventWrapper, declaredAnnotations=[]}])
Arguments: [(io.cucumber.testng.PickleEventWrapperImpl) "Sunday isn't Friday"]

    at org.testng.internal.reflect.DataProviderMethodMatcher.getConformingArguments(DataProviderMethodMatcher.java:45)
    at org.testng.internal.Parameters.injectParameters(Parameters.java:796)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:983)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)

这样做的副作用是,我无法在编写 BDD 时模拟 util 类的静态方法。我是黄瓜 BDD 的新手。任何帮助/指针表示赞赏。

4

1 回答 1

1

在从#help-cucumber-jvm slack 频道获得一些根本原因的帮助后,我能够使用自定义类使用 dataproviders 来测试ng+powermock。例如这个测试失败

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

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.mockito.Matchers.any;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

@PrepareForTest(Util2.class)
public class TestngWithDataproviderTestngTest extends PowerMockTestCase {

    @ObjectFactory
    public org.testng.IObjectFactory getObjectFactory() {
        return new org.powermock.modules.testng.PowerMockObjectFactory();
    }

    private void mockActiveBucket() {
        PowerMockito.mockStatic(Util.class);
        PowerMockito.when(Util.getBucketId(any(Long.class))).thenReturn(3);
    }

    @Test(dataProvider = "users")
    public void testMockStatic(MyTestCaseImpl myTestCase) {
        System.out.println("myTestCase: " + myTestCase);
        System.out.println("static test passed");
        mockActiveBucket();
        Assert.assertTrue(true);
    }

    @DataProvider(name = "users")
    public Object[][] users() {
        return new Object[][]{{new MyTestCaseImpl(5)}};
    }
}

//interface MyTestCase {
//}

class MyTestCaseImpl { //implements MyTestCase{
    int i;

    public MyTestCaseImpl() {
    }

    public MyTestCaseImpl(int i) {
        this.i = i;
    }

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }
}

class Util2 {
    public static int getBucketId(long eventTimestamp) {
        Long minsPast5MinBoundary = (eventTimestamp % TimeUnit.MINUTES.toMillis(5)) / TimeUnit.MINUTES.toMillis(1);
        return minsPast5MinBoundary.intValue();
    }
}

如前所述,这里似乎是一个解决方法的已知问题。希望这可以帮助。

于 2020-05-05T23:36:36.773 回答