junit的运用

junit自带的有junit包 右键点击项目---》build path---》add libraries---》Junit---》next---》选择junit4-----》finish

junit多线程测试 依靠GroboUtils的子项目 下载网址http://groboutils.sourceforge.net/downloads.html下载GroboUtils-5.zip
 解压后lib目录下的GroboUtils-5-core.jar加入到项目就可以运用它的TestRunnable和MultiThreadedTestRunner

(1)要测试一个类中的所有方法,鼠标点中这个类右键-----》new ---》other--》junit--》junit test case ,下一步就会显示列出你要测试的方法,打上勾就finish,打开测试类编辑你要的内容

package andycpp;

import static org.junit.Assert.*;

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

public class tes1Test {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
  System.out.println("@BeforeClass");
 }

 @AfterClass
 public static void tearDownAfterClass() throws Exception {
  System.out.println("@AfterClass");
 }

 @Before
 public void setUp() throws Exception {
  System.out.println("before");
 }

 @After
 public void tearDown() throws Exception {
  System.out.println("@After");
 }


 @Test(timeout=1000)
 public void testT1() {
  assertEquals("1", "1");
  assertNotNull("为空",1);
  assertTrue("不满足", false);
  System.out.println("testT1");
 }

}

(2)带有多个参数的

package andycpp;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
 * @author Administrator
 * 多个参数的测试
 *
 */
@RunWith(Parameterized.class)//定义这个runner是Parameterized 不是junit默认的
public class CalculatorTest {

  String excep;
  String a;
  String b;
  static Calculator cal=new Calculator();
  @Parameters
  public  static Collection   setList(){
   Object[][] objs={{"a1b1","a1","b1"},{"a2b2","a2","b2"},{"a3b3","a3","b3"}};
   return Arrays.asList(objs);
  }
  public CalculatorTest( String excep, String a,String b){
   this.excep=excep;
   this.a=a;
   this.b=b;
  }
 @Test
 public void testAdd() {
  Assert.assertEquals(this.excep, cal.getCon(this.a, this.b));
  System.out.println("test1");
 }

 

}

 

(3)测试有要测试的所有类

package andycpp;

import junit.framework.JUnit4TestAdapter;
import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * @author Administrator
 * 测试所有的类
 * 操作点击myeclipse的new ---》other--》Junit---》Junit Test suite
 *
 */
public class AllTests {

 public static Test suite() {
  TestSuite suite = new TestSuite("Test for andycpp");
  //$JUnit-BEGIN$
  suite.addTest(new JUnit4TestAdapter(Calculator2Test.class));//把类加入TestSuite,一个一个的运行,如运行Calculator2Test后再运行下一个
  suite.addTest(new JUnit4TestAdapter(CalculatorTest.class));
  
  //$JUnit-END$
  return suite;
 }

}

(4)测试多线程

package andycpp;

import net.sourceforge.groboutils.junit.v1.TestRunnable;

/**
 * @author Administrator
 * 一个线程相当于implements Runnabele的效果
 *
 */
public class testR1 extends TestRunnable {

 
 @Override
 public void runTest() throws Throwable {
  System.out.println(Thread.currentThread().getName()+"----开始"); 
  Thread.sleep(100);
     System.out.println(Thread.currentThread().getName()+"----结束");
 }

}


 

package andycpp;

import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable;

import org.junit.Test;

/**
 * @author Administrator
 * junit多线程测试 依靠GroboUtils的子项目 下载网址http://groboutils.sourceforge.net/downloads.html 下载GroboUtils-5.zip
 * 解压后lib目录下的GroboUtils-5-core.jar加入到项目就可以运用它的TestRunnable和MultiThreadedTestRunner
 *
 */
public class Calculator2Test {
 @Test
 public void testDivide() {
  TestRunnable test1 =new testR1();
  TestRunnable test2 =new testR1();
  
  TestRunnable test3 =new testR1();
  TestRunnable test4 =new testR1();
  TestRunnable[] trs={test1,test2,test3,test4};
  MultiThreadedTestRunner runners=new MultiThreadedTestRunner(trs );
  try {
   runners.runTestRunnables();
  } catch (Throwable e) {
   e.printStackTrace();
  }
 }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值