Junit 简单示例(使用TestSuite)

本文介绍了一个使用JUnit进行单元测试的示例,展示了如何通过编写测试用例验证集合是否为空及如何运行测试套件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import junit.framework.*;

import java.util.*;

public class SimpleTest extends TestCase {

    public SimpleTest(String name) {
       
        super(name);
    }
   
   
    /**
     * 写一个测试方法断言期望的结果
     * JUnit推荐的做法是以test作为待测试的方法的开头,这样这些方法可以被自动找到并被测试。 
     */
    public void testEmptyCollection() {
       
        System.out.println("in testEmptyCollection");
        Collection collection = new ArrayList();
        assertTrue(collection.isEmpty());
    }
   
    public void test1(){
       
        System.out.println("in test1");
    }
   
    public void test2(){
       
        System.out.println("in test2");
    }
   
    public void test3(){
       
        System.out.println("in test3");
    }

    public static Test suite(){
       
        //Constructs a TestSuite from the given class.
        //Adds all the methods of the class starting with "test" as test cases to the suite.
        return new TestSuite(SimpleTest.class);
       
    }
   
    public static void main(String[] args) {
       
        junit.textui.TestRunner.run(suite());
    };
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值