Ant integrated junit automated testing and ant integrated junit

Source: Internet
Author: User

Ant integrated junit automated testing and ant integrated junit

1. Use Junit for testing

1. Java Business Code:

Public class HelloWorld {// test returns "world" public String hello () {return "world" ;}// test returns "hello" public String world () {return "hello" ;}// the test is empty. public String nil () {return null ;}// the test is not empty. public String notNil () {return "abc ";} // The test throws an exception public String ext () {return null ;}}

2. Use junit3 to test and inherit from TestCase

Import junit. framework. testCase; public class HelloWorldTest extends TestCase {private HelloWorld helloWorld; @ Overrideprotected void setUp () throws Exception {helloWorld = new HelloWorld (); System. out. println ("helloWorld init");} public void testHello () {String str = helloWorld. hello (); assertEquals ("test world failed", str, "world");} public void testWorld () {String str = helloWorld. world (); assertEquals ("test world failed", str, "hello");} public void testNotNil () {assertNotNull ("the object is empty", helloWorld. notNil ();} public void testNil () {assertNull ("the object is not empty", helloWorld. nil ();} public void testExt () {try {helloWorld. ext (); fail ("no Exception thrown");} catch (NumberFormatException e) {}}@ Overrideprotected void tearDown () throws Exception {System. out. println ("hello world destory"); helloWorld = null ;}}

4. Test with junit4 and use annotations

Import org. junit. after; import org. junit. before; import org. junit. test; import static org. junit. assert. *; // static introduction. assertEquals is compatible with junit3public class HelloWorldTest {private HelloWorld helloWorld; @ Beforepublic void setUp () {helloWorld = new HelloWorld () ;}@ Testpublic void testHello () {String str = helloWorld. hello (); assertEquals ("hello test failed", str, "world") ;}@ Testpublic void testWorld () {String str = helloWorld. world (); assertEquals ("world test failed", str, "hello") ;}@ Testpublic void testNil () {assertNull ("the object is not empty", helloWorld. nil () ;}@ Testpublic void testNotNil () {assertNotNull ("the object is empty", helloWorld. notNil ();} @ Test (expected = NumberFormatException. class) public void testExt () {helloWorld. ext () ;}@ Afterpublic void tearDown () {helloWorld = null ;}}





How does java ant test Junit?

How does ant call/start the junit test class?

Ant execution of junit Testing

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.