技术中心

这里象征着我们的态度和能力

>junit 单元测试使用方法
作者:中国IT实验室    来源:中国IT实验室    发布时间:2012-01-28      浏览次数:6844
分享到:
欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

    junit 是用来做单元测试,最近项目中可能要需要,所以了解了以下!说一下junit的使用!

    一、安装junit

    下载junit.jar放在你工程的编译路径下, ok!这不用解释

    二、使用junit

    使用的几种情况:具体使用间三中的代码注释部分

    1、测试某个类的所有方法

    2、测试某个类的个别方法

    3、测试几个类中的全部方法

    4、测试几个类中的指定方法

    三、 几个已经生成的类、eclipse3.2+myeclipse4.1+j2sdk1.4.2

    以下是被测试的类

 package common;
  import java.io.*;
  public class RunTime
  {
  public boolean executeRunTime(int str)
  {
  /*
  * try { Process prop = Runtime.getRuntime().exec(str); // Process prop =
  * Runtime.getRuntime().exec("cmd/E:ON/C start 1.txt"); } catch
  * (IOException e) { e.printStackTrace(); }
  */
  if (str == 1)
  {
  return true;
  } else
  {
  return false;
  }
  }
  public int reValue(int a ,int b)
  {
  return a+b;
  }
  }

    以下是junit的测试类
 package common;
  import junit.framework.AssertionFailedError;
  import junit.framework.Test;
  import junit.framework.TestCase;
  import junit.framework.Assert;
  import junit.framework.TestSuite;
  public class RunTimeTest extends TestCase
  {
  public static RunTime op = null;
  public RunTimeTest(String name)
  {
  super(name);
  }
  protected void setUp() throws Exception
  {
  super.setUp();
  op = new RunTime();
  }
  protected void tearDown() throws Exception
  {
  super.tearDown();
  }
  /*
  * Test method for common.RunTimeTest.executeRunTime()
  */
  public void testExecuteRunTime()
  {
  // junit.framework.TestResult r = new junit.framework.TestResult();
  try
  {
  Assert.assertEquals(true, op.executeRunTime(1));// 若失败则抛出AssertionFailedError异常
  // throw new Exception( "This is a test.");
  // Assert.fail();
  } catch (Exception e)
  {
  System.out.println("sfsd");
  Assert.fail("fsdf");
  }
  }
  public void testreValue()
  {
  Assert.assertEquals(" i am here!",2,op.reValue(1,2));
  }
  // 可在一个单独类中实现
  public static Test suite()
  {
  TestSuite suite = new TestSuite("ALL TEST");
  /*以下是测试某个具体方法*/
  //suite.addTest(new RunTimeTest("testreValue"));
  suite.addTest(new RunTimeTest("testExecuteRunTime"));
  /*下句是执行指定类中的所有方法*/
  //suite.addTestSuite(RunTimeTest.class);
  return suite;
  }
  public static void main(String[] args)
  {
  // 以下三种方式均可以,具体情况可运行以下,看一下结果
  junit.textui.TestRunner.run(suite());
  // junit.swingui.TestRunner.run(Test.class);
  // junit.awtui.TestRunner.run(Test.class);
  //junit.swingui.TestRunner.run(RunTimeTest.class);
  }
  }
    以下是使用suite的类,可以测试多个
 package common;
  import junit.framework.TestCase;
  import junit.framework.TestSuite;
  import junit.framework.Test;
  import common.RunTimeTest;
  public class TestUnit
  {
  public static Test suite()
  {
  //以下是用来增加单个测试用例
  TestSuite suite = new TestSuite("ALL TEST");
  //以下这句将运行RunTimeTest中被指定的方法,如testreValue
  suite.addTest(new RunTimeTest("testreValue"));
  //以下这句将运行RunTimeTest中的所有测试方法
  //suite.addTestSuite(RunTimeTest.class);
  //以下这句讲运行RunTimeTest.suite()中规定的一组方法
  //suite.addTest(RunTimeTest.suite());
  return suite;
  }
  public static void main(String[] args)
  {
  //以下三种方式均可以,具体情况可运行以下,看一下结果
  // junit.textui.TestRunner.run(TestUnit.class);
  // junit.swingui.TestRunner.run(Test.class);
  // junit.awtui.TestRunner.run(Test.class);
  // junit.swingui.TestRunner.run(TestUnit.class);
  junit.textui.TestRunner.run(suite());
  }
  }
4000-880-989
(24小时热线)
联系客服
微信公众号

官方公众号

小程序

©2008-2022 CORPORATION ALL Rights Reserved. 昆明奥远科技有限公司版权所有 滇ICP备09003328号-1 滇公网安备 53011102000818号 增值电信业务经营许可证号:滇B2-20110045
昆明那家网络公司好,新媒体运营,网站优化,网络推广,网站建设,网页设计,网站设计,网站推广,云南网站公司,昆明新媒体公司,云南网红主播,昆明SEO公司,昆明网站建设,昆明网络推广,昆明网站优化,昆明网站推广,红河网站建设,大理网络公司,曲靖网络公司,丽江网站设计,昭通网络公司,保山大数据服务,智慧高速建设,智慧校园服务,云南IDC服务商,网络安全测评,等保测评,网站关键词排名优化服务,服务客户尽超2000余家,一切尽在奥远科技,服务电话:13888956730