国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

junit單元測試使用方法

2024-07-14 08:44:01
字體:
來源:轉載
供稿:網友

  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());

  }

  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 光山县| 巫山县| 长海县| 汪清县| 广丰县| 蕲春县| 哈尔滨市| 开鲁县| 富宁县| 平乐县| 泰宁县| 马尔康县| 凌源市| 光山县| 无为县| 石泉县| 沙湾县| 海晏县| 绥德县| 湘阴县| 濉溪县| 铁岭县| 马公市| 平武县| 江门市| 宣恩县| 凤城市| 年辖:市辖区| 留坝县| 安远县| 西宁市| 河津市| 邵武市| 南江县| 青海省| 贺州市| 万年县| 芮城县| 资源县| 兴城市| 永昌县|