【问题标题】:TestNG @Factory annotation + not enough knowledge on Dependency InjectionTestNG @Factory 注解 + 没有足够的依赖注入知识
【发布时间】:2013-08-12 09:42:54
【问题描述】:

我只是在学习依赖注入,我根本不明白它的逻辑。更何况,TestNG的要求对我来说总是太离奇了。

我有这个提供数据的类,以及一个需要它的测试类:

public abstract class AppData { // extends Mailer

    @DataProvider(name = "dataProvider")
    public static Object[][] setUp() throws Exception {


        //prepare the data here - code.

        //pass the data to the test case
        Object[][] setUp = new Object[1][4];
        setUp[0][0] = driver;
        setUp[0][1] = wait;
        setUp[0][2] = array;
        setUp[0][3] = array2;   
        return setUp;
    }
}

这是测试类

public class AppTest { 

//this is my attempt to make a dependency injection, completely blindly shooting in the dark
    private AppData appdata;

    public AppTest (
            AppData appdata
        ) {
           this.appdata = appdata;
        }



@Factory(dataProviderClass=com.fmydomain.tests.AppData.class,dataProvider="dataProvider")
    public void oneUserTwoUser(WebDriver driver, WebDriverWait wait, ArrayList<ArrayList<String>> array, ArrayList<ArrayList<String>> array2) throws Exception {

    }
}

我在运行它时遇到了这个错误:

org.testng.TestNGException: 
The factory method class com.f1rstt.tests.AppTest3.oneUserTwoUser() threw an exception
    at org.testng.internal.FactoryMethod.invoke(FactoryMethod.java:93)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:140)

编辑:我也尝试了以下方法:

public class AppTest3 { 

    private AppData appdata = new AppData();

    public AppTest3(AppData appdata) {
        this.appdata = appdata;
    }

    WebDriver driver = AppData.driver;
    WebDriverWait wait = AppData.wait;
    ArrayList<ArrayList<String>> array = AppData.array;
    //ArrayList<ArrayList<String>> array2 = AppData.array2;

    @Parameters({ "driver", "wait", "array" })
    @Test
    public void oneUserTwoUser(WebDriver driver, WebDriverWait wait, ArrayList<ArrayList<String>> array) throws Exception {

但我收到此错误:

org.testng.TestNGException: 
Parameter 'driver' is required by @Test on method oneUserTwoUser but has not been marked @Optional or defined

【问题讨论】:

  • 您是否尝试将 Spring 用于 DI?
  • 我不知道 Spring 或 DI 是什么意思。 @assylias 抱歉,我现在编辑了问题。
  • @J.Kowalski DI=依赖注入/Spring=一个 DI 框架。我已经删除了在这种情况下有点混乱的 DI 标签。
  • 您是否尝试将setup 方法直接包含到您的AppTest 类中?
  • 好吧,我没有使用 DI。我刚刚在网上找到了一些代码,并试图实现 DI,但是我在 TestNG 上遇到了这个问题......

标签: java testng


【解决方案1】:

这里提供了答案,在重构和安装一个DI框架Spring Framework and TestNG: unable to pass some parameters之后

【讨论】:

    猜你喜欢
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2011-11-06
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多