繁体   English   中英

我可以在Spring Context中使用TestNG DataProvider吗,即在与SpringJUnit4ClassRunner一起运行的测试代码中

[英]Can I use TestNG DataProvider in Spring Context i.e. in my Test Code which runswith SpringJUnit4ClassRunner

我在Spring进行一些Junit测试,在我的Test Class中,我必须使用一些测试数据。

我搜索了谷歌,有很多方法,例如- Parameterized类,TestNG Data提供程序, [junit-dataprovider][1]等。

我虽然尝试使用TestNG数据提供程序,并为我的测试数据创建了一个简单的类,如下所示:

@ContextConfiguration(classes = TestConfiguration.class)
public class TestNgDataProvider extends AbstractTestNGSpringContextTests {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestNgDataProvider.class);

    @DataProvider(name = "invoices")
    public static Object[][] dataProviderMethod() {
        return new Object[][]{
                {"201628JP00006097", "201628JP00006098"},
        };
    }

    @Test(dataProvider = "invoices")
    public void testParameterMethod(String invoiceId1, String invoiceId2) {
        LOGGER.info("Start test");
        LOGGER.info("Test data is: {}, {}", invoiceId1, invoiceId2);

    }
}

但是,现在当我尝试在测试类中使用此提供程序时,出现了一个exceptionjava.lang.Exception :没有可运行的方法

我的测试类正在使用runWith SpringJUnit4ClassRunner 任何人都知道如何解决此问题。

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.testng.annotations.Test;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(TestConfiguration.class)
public class TestLax extends LaxBaseTest {
  @Test (dataProvider="invoices", dataProviderClass=TestNgDataProvider.class)
    public void testLarsTamara(String invoiceId1, String invoiceId2) throws Exception {
        LOGGER.info("Test data is: {}, {}", invoiceId1, invoiceId2);

        File file = larsRemote.runLars();
        readAndVerifyLarsFile(file);
    }
}

当然不能!

JUnit无法运行TestNG测试或功能。 但是相反,TestNG能够运行junit测试。

只需使用testng方式运行测试,它应该可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM