Simple use on Junit

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

Junit is a simple tool for test. It is easy to learn. You can find a book called Action In JUnit , but as a beginned you just need to know some basic use of it.

You build a class extends TestCase, add as many public void test methos as you want, with test begin, for instance

public class TestXXXX{

public void testXXX(){

// you code

}

}

in the code part you can use any java code, including System.out.printn() to print information, or use assertXXXX() method JUnit provide.

When you use Runner to eun the test, use Runner.run(TestXXXX.class) in one main method in one class or use command line method.

You also can use TestSuite to add TestCase togather. use add method., you can build a testAll class and add a method as:

public class TestAll{

public static Test Suite(){

TestSuite suit=new TestSuite();

suite.addTestCase(TestXXXX.class); // or suite.add(new TestXXXX());

// add othe TestCases

return suite

}

}

Then you can use Runner.run(TestAll.class) to run all TestCase classes in the suite.

Actually, TestCase and TestSuite all emplements the Test interface, it is a composite pattern, so you also can ass TestSuite to another TestSuite and run it.

It is a easy way to use JUnit.and i think it is enough for simple programe. For more complex use, you can find another reference, such Action in junit, which i mentioned at first. It introduce some response, reauest test as a Http protocal, and other self-developed Test Classes. That's a interesting anduseful book.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有