学习使用TestNG中的注解(2)——@Factory的使用

当通过testng.xml或命令行把测试类传递给TestNG时,TestNG会调用这些测试类的无参构造方法,将这些类实例化,然后执行在每个类中的测试方法。

如果假设某个测试类中构造方法是有参的,那么运行时,TestNG会报出TestNGException异常,因为无法初始化这个类(该类没有无参构造方法),报错见下。

org.testng.TestNGException: 
Can't invoke public void testNGtest.TestFactory.testInitialChar() throws java.io.IOException: either make it static or add a no-args constructor to your class

针对这个问题,我们可以在该测试类中添加无参数的构造方法。但是这样做的话很不方便,这意味着无法灵活的、动态的创建该测试类的实例,没有实际的意使用义。

这个时候就需要使用TestNG的@Factory注解了。

被@Factory注解的方法必须是返回一个对象数组的方法。而且这些返回的对象数组中的对象必须包含TestNG annotation的类的实例。

我们可以把@Factory方法和@Test方法放在同一个类中,因为一方面@Factory方法只会被调用一次,另一方面@Factory方法优先于@Test方法和配置方法被调用,只有当所有的@Factory方法被调用之后,TestNG才执行@Test方法和配置方法。

现在用一个简单的例子来验证@Factory方法的一些特性与熟悉@Factory方法的基础使用方法。

假设我们要做如下测试工作:针对一组给定文件路径名称的txt文件,我们需要验证这些txt文件内容中的首字母是否是‘a’。

可以先创建测试类,该类中有一个有参构造方法,一个获取指定路径的文件内容的首字母的实例方法,和一个@Test方法,用于比较指定路径的文件内容的首字母是否与预期相同。

public class TestFactory{

    private String path;
    private char expectedInitialChar;
    
public TestFactory(String path, char expected){
        this.path = path;
        this.expectedInitialChar = expected;
    }
    
    
    @Test
    public void testInitialChar() throws IOException {
        Assert.assertEquals(this.getInitialChar(path), this.expectedInitialChar);
    }
    
    
    public char getInitialChar(String path) throws IOException{
        File file = new File(path);
        FileInputStream is = new FileInputStream(file);
        byte[] byteBuffer = null;
        try {
            byteBuffer = new byte[is.available()];
        } catch (IOException e) {
            e.printStackTrace();
        }
        int read = 0;
        while ((read = is.read(byteBuffer)) != -1) {
            System.out.write(byteBuffer, 0, read);
        }
        return (char) byteBuffer[0];
    }

}

如果此时直接使用TestNG运行该测试类会报错

org.testng.TestNGException: 
Can't invoke public void testNGtest.TestFactory.testInitialChar() throws java.io.IOException: either make it static or add a no-args constructor to your class

所以我们再到该测试类中添加一个@Factory方法,批量生成测试实例,代码见下:

    @Factory
    public static Object[] create() throws IOException{
        List<TestFactory> result = new ArrayList<TestFactory>();
        
        String[] paths = {"d:/a.txt","d:/b.txt"};
        char expected = 'a';
        for(String path:paths){
            result.add(new TestFactory(path, expected));
        }        
        return result.toArray();
    }

这个时候我们再使用TestNG运行该测试类就OK啦

PASSED: File's path: d:/a.txt
FAILED: File's path: d:/b.txt
java.lang.AssertionError: expected [a] but found [b]
    at org.testng.Assert.fail(Assert.java:94)
    at org.testng.Assert.failNotEquals(Assert.java:494)
    at org.testng.Assert.assertEquals(Assert.java:123)
    at org.testng.Assert.assertEquals(Assert.java:328)
    at org.testng.Assert.assertEquals(Assert.java:338)
    at testNGtest.TestFactory.testContent(TestFactory.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)


===============================================
    Default test
    Tests run: 2, Failures: 1, Skips: 0
===============================================
原文地址:https://www.cnblogs.com/zhaochifan/p/5216383.html