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