之前在测试中一直使用testNG的@Test注解都很顺利没有碰到什么问题,今天突然遇到@Test不能用的情况,运行后提示:

org.testng.TestNGException: 

Can't invoke public void testautomation.debugAssertion.testdebug(): either make it static or add a no-args constructor to your class

赶紧查看测试类是不是什么地方写的不对,函数调用关系是不是写对了,检查后代码并没有问题,于是上网查资料后恍然大悟,是因为测试类没有被实例化导致@Test的方法不能正常调用。以前没碰到问题是因为测试类没有定义有参构造函数。

分析后发现:

在run as TestNG test的时候,TestNG会调用测试用例所在测试类的无参构造方法,从而将测试类实例化,然后执行在每个类中的测试方法。但是我的测试类只有一个有参构造函数,TestNG不能实例化类。

于是继续找解决办法,发现@Factory只会被调用一次,而且@Factory方法优先于@Test方法和配置方法被调用,只有当所有的@Factory方法被调用之后,TestNG才执行@Test方法和配置方法。

既然TestNG无法实例化对象,那么我们就用@Factory来帮它完成这个任务。

代码也比较简单,如下:

         @Factory
public static Object[] create() throws IOException{
List<debugAssertion> result = new ArrayList<debugAssertion>();
Assertion as = new Assertion();
result.add(new debugAssertion(as));
return result.toArray();

将这个方法加入到测试类中后运行run as TestNG test,运行成功!!!

[TestNG] Running:
C:\Users\IBM_ADMIN\AppData\Local\Temp\testng-eclipse-1577888796\testng-customsuite.xml 2017-03-16 11:14:19 testautomation.Assertion : verify success : 1
Thread-1 : true
[]
PASSED: testdebug on Thread[Thread-0,5,main] ===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

TestNG注解使用技巧 - @Factory的更多相关文章

  1. 自动化测试框架selenium+java+TestNG——TestNG注解、执行、测试结果和测试报告

    TestNG是java的一个测试框架,相比较于junit,功能更强大和完善,我是直接学习和使用的TestNG就来谈下TestNG的一些特点吧. TestNG的特点 注解 TestNG使用Java和面向 ...

  2. selenium2中的TestNg注解和数据驱动的简介及使用

    TestNg常用注解介绍,先来张图: 先看一下,以上各个注释的运行次序: @Test 表示的意义:    1.表示示该方法是一个测试方法,在运行时,会自动的运行有@Test注脚的方法. 示例: @Be ...

  3. TestNG系列(二)TestNG注解

    前言 TetsNG提供了很多注解,允许测试人员灵活地组织测试用例 一.@Test @Tets是TestNG的核心注解,被注解的方法,表示为一个测试方法. description属性 @Test(des ...

  4. testNG 注解使用说明

    1.TestNG常用注解 @BeforeSuite 标记的方法:在某个测试套件(suite)开始之前运行 @BeforeTest 在某个测试(test)开始之前运行 @BeforeClass 在某个测 ...

  5. testng 注解

    testng.xml suite(套件):  由一个或多个测试组成 test(测试):  由一个或多个类组成 class(类):  一个或多个方法组成 @BeforeSuite: 在某个测试套件开始之 ...

  6. 超详细 Spring @RequestMapping 注解使用技巧

    @RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...

  7. Spring @RequestMapping 注解使用技巧

    @RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...

  8. 超详细 Spring @RequestMapping 注解使用技巧 (转)

    @RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...

  9. Spring MVC中@RequestMapping注解使用技巧(转)

    @RequestMapping是Spring Web应用程序中最常被用到的注解之一.这个注解会将HTTP请求映射到MVC和REST控制器的处理方法上. 在这篇文章中,你将会看到@RequestMapp ...

随机推荐

  1. Gradle sync failed: Cause: org.gradle.logging.StyledTextOutput$Style Consult IDE log for more details

    环境 Android studio 3.0 导入开源中国: ... dependencies { //noinspection GradleDependency classpath 'com.andr ...

  2. 中南oj 1213: 二叉树结点公共祖先

    1213: 二叉树结点公共祖先 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 159  Solved: 87 [Submit][Status][Web ...

  3. 4、springboot之全局异常捕获

    1.新建一个springboot项目 添加一个全局异常的类 import org.springframework.web.bind.annotation.ControllerAdvice; impor ...

  4. ExceptionHelper异常工具类

    using System;using System.Collections.Generic;using System.Text; namespace JiaWel.Utilities{ public ...

  5. 响应式(2)——bootstrap的响应式

    <meta name="viewport" content="width=device-width,user-scalable=no"/> < ...

  6. zookeeper安装及使用

    0. 下载并安装 1. 开机启动 cd  /etc/rc.d/init.d touch zookeeper vi zookeeper #!/bin/bash #chkconfig #descripti ...

  7. Ajax与jsonp

    1.ajax的概念 AJAX = Asynchronous Javascript And XML (AJAX  =  异步  javascript  和 xml) AJAX是一种无需重新加载整个网页的 ...

  8. Java 文件上传与下载、email

    1. 文件上传与下载 1.1 文件上传 文件上传,要点: 前台: 1. 提交方式:post 2. 表单中有文件上传的表单项: <input type="file" /> ...

  9. dom操作排他思想

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. js if语句只写一个参数是什么意思?

    如 var a=0:if(!a){...}; avascript中以下值会被转换为false false undefined null 0 -0 NaN ""