[英]How to use spring autowiring in a testng factory class
Currently I have a factory class that looks like this:目前我有一个工厂类,如下所示:
@ContextConfiguration(classes = BeanConfig.class)
public class FactoryClass extends AbstractTestNGSpringContextTests {
@Autowired
public Bean bean;
@Factory(dataProvider="dataProvider")
public Object[] createTest(int a, int b) {
return new Object[]{new FactoryTestClass(a, b)};
}
@DataProvider(name="dataProvider",parallel=true)
public Object[][] passInts(){
bean.method();
return new Object[][]{{2,2},{2,3},{2,4},{2,4}};
}
@BeforeSuite
public void beforeSuite(){
System.out.println("before suite");
}
}
My goal is to use spring's autowiring feature so i can use a bean to help generate some test data for the data provider.我的目标是使用 spring 的自动装配功能,这样我就可以使用 bean 来帮助为数据提供者生成一些测试数据。 However in my attempt the spring context never initialises.
然而,在我的尝试中,spring 上下文永远不会初始化。 Does anyone know what I might be doing wrong, or is there another approach I can take?
有谁知道我可能做错了什么,或者我可以采取其他方法吗?
Thank you kindly, Jason谢谢你,杰森
I had some similar issue: my test folder was located outside directory main, so, after I marked it as the Test Source Resource (in Intellij IDE) it started to work.我有一些类似的问题:我的测试文件夹位于 main 目录之外,因此,在我将其标记为测试源资源(在 Intellij IDE 中)之后,它开始工作。 Hope it helps.
希望能帮助到你。
I would suggest to locate @DataProvider
is same class as @Test
method.我建议找到
@DataProvider
与@Test
方法相同的类。 I never had a problem with this approach.这种方法我从来没有遇到过问题。
Having various @Test
methods and various dataProvider
s in one test class is valid usage.在一个测试类中拥有各种
@Test
方法和各种dataProvider
是有效的用法。 @Test
method will specify which dataProvider is used in @Test
annotation parameter. @Test
方法将指定在@Test
注释参数中使用哪个 dataProvider。
Example:例子:
@DataProvider(name="dataProvider",parallel=true)
public Object[][] passInts(){
bean.method();
return new Object[][]{{2,2},{2,3},{2,4},{2,4}};
}
@Test(dataProvier="dataProvider")
public test(int param1, int param2){
//...
}
尝试将loader=AnnotationConfigContextLoader.class
添加到 ContextConfiguration。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.