Java combined with TestNG assertion verify (assertion failure continues without interruption)

Source: Internet
Author: User
Tags try catch testng

Principle:

1. Self constructs an assertion class, assert.assertequals to try catch.

2. Use the TESTNG Listener class to analyze after the test method has finished running.

Code:

Assertion class:

Package Com.demo;import Java.util.arraylist;import Java.util.list;import Org.testng.assert;public class Assertion {  public static Boolean flag = True;public static list<error> errors = new arraylist<error> ();p ublic static void Verifyequals (object actual, object expected) {Try{assert.assertequals (actual, expected);} catch (Error e) {errors.add (e); flag = false;}} public static void Verifyequals (object actual, object expected, String message) {Try{assert.assertequals (actual, expected, message);} catch (Error e) {errors.add (e); flag = FALSE;}}}

Listener class:

Package Com.demo;

Import java.util.ArrayList;
Import java.util.List;

Import Org.testng.ITestResult;
Import Org.testng.TestListenerAdapter;

public class Assertionlistener extends Testlisteneradapter {

@Override
public void Onteststart (Itestresult result) {
Assertion.flag = true;
Assertion.errors.clear ();
}

@Override
public void Ontestfailure (Itestresult tr) {
This.handleassertion (TR);
}

@Override
public void ontestskipped (Itestresult tr) {
This.handleassertion (TR);
}

@Override
public void Ontestsuccess (Itestresult tr) {
This.handleassertion (TR);
}

private int index = 0;

private void Handleassertion (Itestresult tr) {
if (! Assertion.flag) {
Throwable throwable = tr.getthrowable ();
if (throwable==null) {
Throwable = new Throwable ();
}
stacktraceelement[] traces = Throwable.getstacktrace ();
stacktraceelement[] Alltrace = new Stacktraceelement[0];
for (Error e:assertion.errors) {
stacktraceelement[] errortraces = E.getstacktrace ();
stacktraceelement[] et = this.getkeystacktrace (tr, errortraces);
stacktraceelement[] message = new Stacktraceelement[]{new stacktraceelement ("Message:" +e.getmessage () + "in method:", t R.getmethod (). Getmethodname (), Tr.gettestclass (). Getrealclass (). Getsimplename (), index)};
index = 0;
Alltrace = This.merge (alltrace, message);
Alltrace = This.merge (Alltrace, ET);
}
if (traces!=null) {
traces = This.getkeystacktrace (tr, traces);
Alltrace = This.merge (Alltrace, traces);
}
Throwable.setstacktrace (Alltrace);
Tr.setthrowable (Throwable);
Assertion.flag = true;
Assertion.errors.clear ();
Tr.setstatus (itestresult.failure);
}
}

Private stacktraceelement[] Getkeystacktrace (Itestresult tr, stacktraceelement[] stacktraceelements) {
list<stacktraceelement> ets = new arraylist<stacktraceelement> ();
for (Stacktraceelement stacktraceelement:stacktraceelements) {
if (Stacktraceelement.getclassname (). Equals (Tr.gettestclass (). GetName ())) {
Ets.add (stacktraceelement);
index = Stacktraceelement.getlinenumber ();
}
}
stacktraceelement[] et = new stacktraceelement[ets.size ()];
for (int i = 0; i < et.length; i++) {
Et[i] = Ets.get (i);
}
Return et;
}

Private stacktraceelement[] Merge (stacktraceelement[] traces1, stacktraceelement[] traces2) {
stacktraceelement[] ste = new Stacktraceelement[traces1.length+traces2.length];
for (int i = 0; i < traces1.length; i++) {
Ste[i] = Traces1[i];
}
for (int i = 0; i < traces2.length; i++) {
Ste[traces1.length+i] = Traces2[i];
}
return Ste;
}
}

Test class:

Package Com.demo;import Org.testng.annotations.listeners;import org.testng.annotations.Test; @Listeners ({ Com.demo.AssertionListener.class}) public class Test14 {@Testpublic void TestAssert3 () {assertion.verifyequals (2, 3, " Compare whether two numbers are equal: "); Assertion.verifyequals (1, 2, "Compare two numbers equal:");} @Testpublic void TestAssert4 () {assertion.verifyequals (4, 3, "Compare two numbers equal:"); Assertion.verifyequals (2, 2, "Compare two numbers equal:");}}

Java combined with TestNG assertion verify (assertion failure continues without interruption)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.