Jtester之TestNG:@Factory注解

来源:互联网 发布:win7 64位 知乎 编辑:程序博客网 时间:2024/05/28 16:08
@Before...、@After...、@DataProvider、@Test这些注解一看名称就能理解,@Factory就相比晦涩了。有篇博文,给出的案例比较全,http://howtodoinjava.com/testng/testng-factory-annotation-tutorial/?utm_source=tuicool&utm_medium=referral。总结来说,@Factory就是以实例化测试类的方式来驱动测试。下面结合项目实例,写了一个用到@Factory的测试类。

1、接口

dubbo接口1:返回 true或false,入参为arg1-String类型,arg2-String类型,arg3-String类型。
dubbo接口2:返回引用类型BankListResult,入参为arg1-String类型,arg2-String类型,arg3-String类型,arg4-String类型。
(接口1、2中的arg1、arg2、arg3相同)

2、测试类设计

属性为arg1、arg2、arg3、arg4,用@Factory实现一个带参构造方法,写2个@Test方法分别测试接口1和接口2,@DataProvider给工厂提供测试数据。

3、参考代码

@SpringApplicationContext({ "spring-dubbo.xml" })public class FactoryDemo extends JTester {    private String arg1;    private String arg2;    private String arg3;    private String arg4;    // 成员方法1:获取接口1预期响应    public static boolean getFnExp1(String arg1, String arg2,String arg3) throws SQLException {        boolean expected1;        String sql = "......";        ......        return expected1;    }    //成员方法:获取接口1预期响应    public BankListResult getFnExp2(String arg1,String arg2, String arg3, String arg4)            throws SQLException {        BankListResult expected2 = new BankListResult();        String sql = "......";        ......        return expected2;    }    @SpringBeanByName("interfaceService1")    com.api.INTERFACE1 int1;    @SpringBeanByName("interfaceService2")    com.api.INTERFACE2 int2;    @Factory(dataProvider = "testdata")    public FactoryDemo(String arg1, String arg2, String arg3,            String arg4) {        this.arg1 = arg1;        this.arg2 = arg2;        this.arg3 = arg3;        this.arg4 = arg4;    }    @Test    public void fnTest1() throws SQLException {        boolean expected = getFnExp1(arg1, arg2, arg3);        boolean actual = int1.fn1(arg1, arg2, arg3);        want.bool(actual).isEqualTo(expected);    }    @Test    public void fnTest2() throws SQLException {        BankListResult expected = getFnExp2(arg1, arg2,arg3, arg4);        BankListResult actual = int2.fn2(arg1, arg2,arg3, arg4);        want.object(actual).eqByReflect(expected);    }    @DataProvider(name = "testdata")    public static Object[][] testdata() {        return new Object[][] {        new Object[] { "310000370023", "1.0.0", "GWP", "100" } };    }}
0 0
原创粉丝点击