• junit 用法实例


    package com.zy.junit.test;

    import org.junit.After;
    import org.junit.AfterClass;
    import org.junit.Before;
    import org.junit.BeforeClass;
    import org.junit.Ignore;
    import org.junit.Test;

    import com.zy.service.ServiceTest;

    /**
     *
     * junit 3有一些比较霸道的地方,表现在: 1.单元测试类必须继承自TestCase。2.要测试的方法必须以test开头。
     * 推荐使用junit 4 
     * (1)被测试的类必须放在包里(2)返回值必须为void,而且不能有任何入参,否则运行时异常
     * @author Administrator
     *
     */
    public class Junit4Test
    {

     // 每个测试方法执行之前都要执行一次
     @Before
     public void before()
     {
      System.out.println("before");
     }

     // 每个测试方法执行之后要执行一次
     @After
     public void after()
     {
      System.out.println("after");
     }

     // 只在测试类实例化执行@BeforeClass方法一次,必须声明static
     @BeforeClass
     public static void beforeClass()
     {
      System.out.println("beforeClass");
     }

     // 当所有测试执行完毕之后,执行@AfterClass一次,必须声明static
     @AfterClass
     public static void afterClass()
     {
      System.out.println("afterClass");
     }

     // @Test元数据中的expected属性。expected属性的值是一个异常的类型,相当于try catch,只是吃掉了此异常
     @Test(expected = ArithmeticException.class)
     public void testExpected()
     {
      int a = 10 / 0;
      System.out.println(a);
     }

     // 传入了一个时间(毫秒)给测试方法,如果测试方法在制定的时间之内没有运行完,则测试也失败。
     @Test(timeout = 500)
     public void testTimeout()
     {
      try
      {
       Thread.sleep(480);
      } catch (InterruptedException e)
      {
       e.printStackTrace();
      }
      System.out.println("timeout test");
     }

     // 测试方法在测试中会被忽略,用于:测试的方法还没有实现时,保证不阻碍其他方法的测试
     @Ignore
     public void testIgnore()
     {
      System.out.println(new ServiceTest().getString("hello"));
     }

     // 使用@Test标注,表明这是一个测试方法
     @Test
     public void testGetString()
     {
      System.out.println(new ServiceTest().getString("hello"));
     }

     @Test
     public void testGetString2()
     {
      System.out.println(new ServiceTest().getString("hello2"));
     }

    }

  • 相关阅读:
    Oracle 备份与恢复介绍
    Oracle 监听器
    ORA-01041: 内部错误,hostdef 扩展名不存在
    NIO读写文件并加锁
    ActiveMQ消息生产消费流程
    金额,有效值等保留小数位处理
    JVM
    Linux架构分布式集群之基础篇
    Vue.js 开发实践:实现精巧的无限加载与分页功能
    Mysql 查看连接数,状态
  • 原文地址:https://www.cnblogs.com/qqzy168/p/3338596.html
Copyright © 2020-2023  润新知