A simple Java unit test Framework __java

Source: Internet
Author: User
Tags assert

The

is known as a framework that is a bit exaggerated, and is primarily handy for unit testing when using sublime to develop Java. Testcase.java

Import java.util.*;
Import Java.lang.reflect.Method;

Import Java.lang.reflect.Modifier; Public abstract class TestCase {/* Determines whether the actual result is equal to the expectation and returns the result of the decision with the run location information @ Actual actual result @ Expect expected result return value: Check the result String remark: Cache check result/public string assertequal (object actual, object expect) {Throwable ex = new Throwa
        BLE ();

        stacktraceelement[] stackelements = Ex.getstacktrace ();
            TestResult r = new TestResult (actual.equals (expect), Stackelements[1].getfilename (),
        Stackelements[1].getlinenumber ());
        This.results.add (R); String s = String.Format ("%s, line%d,%s", R.filename, R.line, actual.equals (expect)?
        "SUCCESS": "FAILED");
        System.out.println (s);
    return s;

    Public ArrayList < TestResult > results = new ArrayList < TestResult > (); /* UnitTest executes all test methods of the use case and outputs the statistic result return value: Statistic result String */public string unittest () {results = new ArrayList < TesTResult > (); Iterate through the public method Class < of all the case objects beginning with test?
        > CLS = This.getclass ();
        Method[] methods = Cls.getdeclaredmethods ();
                For (methods M:methods) {if (M.getname (). StartsWith ("test")) {//dynamic Invoke test method
                try {m.invoke (this);
        The catch (Exception e) {}}}//statistic failed check int failed = 0; for (TestResult tr:this.results) {if (!TR).
            Result) {failed = 1;
        } String s = String.Format ("Tests:%d, Failed:%d", this.results.size (), Failed);
        System.out.println (s);
    return s;
        public static void Main (string[] args) {Testcaseex c = new Testcaseex ();

        String s = c.assertequal (1, 1);
        TestResult r = new TestResult (True, "Testcase.java", 61);
        ArrayList < TestResult > rs = new ArrayList < TestResult > (); Rs.add(r);
        Assert S.equals ("Testcase.java, Line, SUCCESS");

        Assert C.results.equals (RS); Class <?
        > CLS = C.getclass ();
        Assert Cls.getname (). Equals ("Testcaseex");
        The test obtains the class method method[] methods = Cls.getdeclaredmethods (); For [method M:methods] {if (M.getname (). Equals ("foo")) {try {assert (in

                T) M.invoke (c) = = 100;
        catch (Exception e) {}} s = C.unittest ();
        rs = new ArrayList < TestResult > ();
        R = New TestResult (True, "Testcase.java", 98);
        Rs.add (R);
        R = new TestResult (False, "Testcase.java", 102);

        Rs.add (R);
        Assert S.equals ("Tests:2, failed:1");

    Assert C.results.equals (RS);
    Class Testcaseex extends TestCase {public void Testfoo () {this.assertequal (1, 1);
    public void TestFoo2 () {this.assertequal (1, 0); } publicint foo () {return 100;
 }
}
Testresult.java
public class TestResult {public
    TestResult (boolean ret, String fileName, int line) {this
        . result = RET;
        This.filename = FileName;
        This.line = line;
    }

    public boolean result; Test result public
    String fileName;//test class name public
    int line;//assertequal The call location of the test function where the statement is located public

    static void main (S Tring[] args {
        TestResult tr = new TestResult (True, "TT", MB);
        Assert tr. result;
        Assert tr.filename = = "tt";
        assert tr.line = =;

        TestResult TR2 = new TestResult (True, "tt", m);
        Assert Tr.equals (TR2);
    }

    public boolean equals (Object o) {
        if (!) ( o instanceof TestResult)) {return
            false;
        }
        TestResult tr = (testresult) o;
        return this. result = = tr. Result && This.filename = = Tr.filename && This.line = = Tr.line;
    }

Using Code
public class Demo {public
    static void Main (string[] args) {
        teststring t = new teststring ();
        T.unittest ();
    }

Class TestString extends TestCase {public
    void Teststringfunc () {
        String s = "abc";
        This.assertequal (S.startswith ("a"), true);
        This.assertequal (S.indexof ("B"), 1);
    }

Output results:

Demo.java, line one, SUCCESS
Demo.java, line, SUCCESS
tests:2, failed:0
[finished in 1.8s]

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.