JUnit总结

JUnit总结

                                      

 

      研习了一会儿,写了几个例子,算是明白JUnit的用法了。很奇怪网上的很多文章总是喜欢把简单的问题复杂化……我总结一个简单的,呵呵从代码开始

第一个例子:最简单的HelloWorld的测试例子

待测类:HelloWorld类(最简单的,省去了注释)

package com.shan.testJUnit;

public class HelloWorld {
 public String say(){
  return("Hello World");
 }

}

测试类:TestHelloWorld类:

package com.shan.testJUnit;

import junit.framework.TestCase;

public class TestHelloWorld extends TestCase {

 public static void main(String[] args) {
  junit.textui.TestRunner.run(TestHelloWorld.class);
 }
 
 public void testSay(){
  HelloWorld hello = new HelloWorld();
  assertEquals("Hello World",hello.say());
 }
}

对该例子的说明:

   处理步骤:创建继承TestCase的类

                在该类中为待测类定义测试方法,使用Assert类中的断言方法判断(自己看看 JUnit的javadoc)

                在main方法中运行该类

第二个例子:使用suite的例子

待测类:

package com.shan.test;

public class ForTest {
 public ForTest(){
 }

 public int add(int i,int j){
  return i+j;
 }

}

测试用例:

package com.shan.test;

import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.framework.Test;

public class TestForTest extends TestCase {
 
 public TestForTest(){
  
 }
 
 public static void main(String[] args) {
  junit.textui.TestRunner.run(suite());
 }

 public void testAdd() {
  ForTest forTest=new ForTest();
  assertEquals(6,forTest.add(2,4));
 }
 
 public static Test suite() {

  TestSuite testSuite = new TestSuite();
  testSuite.addTestSuite(TestForTest.class);

  return testSuite;
    }

}

对该例子说明:

suite是做什么用的?:用最通俗的语言,Suite让你能够将多个测试放在一块进行(使用addTestSuite可以将多个测试用例类放在一起)

使用办法:实现public static Test suite() 方法,并在main中使用junit.textui.TestRunner.run(suite());来运行测试
 

第三个例子:使用Fixture的例子

待测类1

package com.shan.test;

public class ForTest {
 
 public ForTest(){  
 }

 public int add(int i,int j){
  return i+j;
 }
}

待测类2:

package com.shan.test;

public class ForTest2 {
 public ForTest2(){
   }
 
 public int subtract(int i ,int j){
  return (i-j);
 }

}

测试用例类:

public class TestForTest extends TestCase {
 
 private ForTest test1;
 private ForTest2 test2;
 protected void setUp() throws Exception {
  super.setUp();
  test1 = new ForTest();
  test2 = new ForTest2(); 
 }

 protected void tearDown() throws Exception {
  super.tearDown();

  //此处添加“善后”处理的代码
 }
 
 public void testAdd() { 
  assertEquals(6,test1.add(2,4));
 }
 public void testSubtract(){
  assertEquals(3,test2.subtract(7,4));
 }
 
 public static void main(String[] args) {
  junit.textui.TestRunner.run(suite());
 }
 
 public static Test suite(){
  TestSuite suite = new TestSuite();
  suite.addTestSuite(TestForTest.class);
  return suite;
 }

}

对该例子的说明:

Fixture是什么?:Fixture就是用来对被测试类进行处理,在setUp中初始化,在tearDown中“善后”处理。

执行顺序:

setUp()
testAdd()
tearDown()
setUp()
testSubtract()
tearDown()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值