首页 > 老文章 > 正文

Simple use on Junit

Simple use on Junit

Simple use on Junit
发布时间:2006-03-08 22:42        来源:        作者:

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 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 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.