=======================测试示例============================= |
=======================被测试类============================= |
package com.fengke; |
import org.junit.Test; |
public class ReadNumbers { |
|
private int a; |
private int b; |
static int c; |
static { |
int c= 0 ; |
} |
|
public int max(){ |
c = b; |
if (a>b){ |
c=a; |
} |
return c; |
|
} |
public ReadNumbers() { |
super (); |
// TODO Auto-generated constructor stub |
} |
public ReadNumbers( int a, int b) { |
super (); |
this .a = a; |
this .b = b; |
} |
|
} |
====================== 基本测试========================= |
package com.test; |
import static org.junit.Assert.assertEquals; |
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.fengke.ReadNumbers; |
/** |
* 测试 |
* @author 锋客 |
* |
* @Test:将一个普通的方法修饰成为一个测试方法 |
* @Test(expected=XX.class) |
* @Test(timeout=毫秒 ) |
* @BeforeClass:它会在所有的方法运行前被执行,static修饰 |
* @AfterClass:它会在所有的方法运行结束后被执行,static修饰 |
* @Before:会在每一个测试方法被运行前执行一次 |
* @After:会在每一个测试方法运行后被执行一次 |
* @Ignore:所修饰的测试方法会被测试运行器忽略 |
* @RunWith:可以更改测试运行器 org.junit.runner.Runner |
* |
* |
*注意: |
* 1.Failure一般由单元测试使用的断言方法判断失败所引起的,这经表示 测试点发现了问题 |
* ,就是说程序输出的结果和我们预期的不一样。 |
* 2.error是由代码异常引起的,它可以产生于测试代码本身的错误,也可以是被测试代码中的 |
* 一个隐藏的bug |
* 3.测试用例不是用来证明你是对的,而是用来证明你没有错。 |
* |
*/ |
public class NumberTest { |
/* |
* @BeforeClass |
* 在测试方法运行前执行 |
* 执行一遍,在整体运行前执行 |
* 通常用来对资源的清理,如关闭数据库的连接 |
*/ |
@BeforeClass |
public static void setUpBeforeClass() throws Exception { |
System.out.println( "BeforeClass" ); |
} |
|
/* |
* @BeforeClass |
* 在测试方法运行后执行 |
* 执行一遍,在整体运行后执行 |
*/ |
@AfterClass |
public static void tearDownAfterClass() throws Exception { |
System.out.println( "AfterClass" ); |
} |
/* |
* @Before |
* 在测试方法运行前执行 |
* 有几个测试,运行几遍 |
* 该方法不能是静态的 |
*/ |
@Before |
public void beforMethod(){ |
System.out.println( "before" ); |
} |
/* |
* @After |
* 在测试方法运行后执行 |
* 有几个测试,运行几遍 |
* 该方法不能是静态的 |
*/ |
@After |
public void afterMethod(){ |
System.out.println( "after" ); |
} |
/* |
* 测试内容 |
* 1.测试方法上必须使用@Test进行修饰 |
* 2.测试方法必须使用public void 进行修饰,不能带任何的参数 |
* 3.新建一个源代码目录来存放测试代码 |
* 4.测试类的包应该和被测试类保持一致 |
* 5.测试单元中的每个方法必须可以独立测试,测试方法间不能有任何的依赖 |
* 6.测试类使用Test作为类名的后缀(不是必须) |
* 7.测试方法使用test作为方法名的前缀(不是必须) |
*/ |
@Test |
public void testMax1() { |
System.out.println( "***************************" ); |
System.out.println( this .getClass().getName()); |
assertEquals( 4 , new ReadNumbers( 2 , 4 ).max()); |
System.out.println( new ReadNumbers( 2 , 4 ).max()); |
System.out.println( "***************************" ); |
} |
@Test |
public void testMax2(){ |
System.out.println( "***************************" ); |
System.out.println( this .getClass().getName()); |
System.out.println( "***************************" ); |
} |
/* |
* 对比 |
* (timeout=2000)限制时间,防止死循环 |
* @Ignore("...")忽略该测试方法,根本不执行,不会运行before和after |
*/ |
@Ignore ( "..." ) |
@Test (timeout= 2000 ) |
public void testWhile1() { |
System.out.println( "***************************" ); |
while ( true ) { |
System.out.println( "run forever..." ); |
} |
//System.out.println("***************************"); |
} |
|
@Test (timeout= 50 ) |
public void testWhile2() { |
System.out.println( "***************************" ); |
while ( true ) { |
System.out.println( "run forever..." ); |
} |
//System.out.println("***************************"); |
} |
/* |
* 对比 |
* expected=ArithmeticException.class |
* 异常处理 |
*/ |
@Test (expected=ArithmeticException. class ) |
public void testDivide1() { |
assertEquals( 3 , 6 / 0 ); |
} |
@Test |
public void testDivide2() { |
assertEquals( 3 , 6 / 0 ); |
} |
|
} |
==========================ParameterTest=========================== |
package com.test; |
import static org.junit.Assert.*; |
import java.util.Arrays; |
import java.util.Collection; |
import org.junit.Test; |
import org.junit.runner.RunWith; |
import org.junit.runners.Parameterized; |
import org.junit.runners.Parameterized.Parameters; |
import com.fengke.ReadNumbers; |
@RunWith (Parameterized. class ) |
public class ParameterTest { |
/* |
* 1.更改默认的测试运行器为RunWith(Parameterized.class) |
* 2.声明变量来存放预期值 和结果值 |
* 3.声明一个返回值 为Collection的公共静态方法,并使用@Parameters进行修饰 |
* 4.为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值 |
*/ |
int expected = 0 ; |
int input1 = 0 ; |
int input2 = 0 ; |
/* |
* @Parameters |
* 静态的方法 |
* |
*/ |
@Parameters |
public static Collection<Object[]> t() { |
return Arrays.asList( new Object[][]{ |
{ 2 , 1 , 2 }, |
{ 3 , 2 , 2 } |
}) ; |
} |
//构造器 |
public ParameterTest( int expected, int input1, int input2) { |
this .expected = expected; |
this .input1 = input1; |
this .input2 = input2; |
} |
@Test |
public void test() { |
assertEquals(expected, new ReadNumbers(input1,input2).max()); |
} |
} |
=======================测试套件=========================== |
package com.test; |
import static org.junit.Assert.*; |
import org.junit.After; |
import org.junit.AfterClass; |
import org.junit.Before; |
import org.junit.BeforeClass; |
import org.junit.Test; |
import org.junit.runner.RunWith; |
import org.junit.runners.Suite; |
import com.fengke.ReadNumbers; |
/** |
* 测试套件 |
* @author 锋客 |
* |
* |
*/ |
@RunWith (Suite. class ) |
@Suite .SuiteClasses({Task1. class ,Task2. class ,Task3. class }) |
public class SuiteTest { |
/* |
* 1.测试套件就是组织测试类一起运行的 |
* |
* 写一个作为测试套件的入口类,这个类里不包含其他的方法 |
* 更改测试运行器Suite.class |
* 将要测试的类作为数组传入到Suite.SuiteClasses({}) |
*/ |
@BeforeClass |
public static void setUpBeforeClass() throws Exception { |
System.out.println( "测试套件开始执行" ); |
} |
@AfterClass |
public static void tearDownAfterClass() throws Exception { |
System.out.println( "测试套件执行完毕" ); |
} |
|
//以下内容不会执行 |
@Before |
public void beforMethod(){ |
System.out.println( "before" ); |
} |
@After |
public void afterMethod(){ |
System.out.println( "after" ); |
} |
@Test |
public void maxTest(){ |
assertEquals( 5 , new ReadNumbers( 5 , 2 ).max()); |
} |
|
} |
=============================== |
package com.test; |
import static org.junit.Assert.*; |
import org.junit.AfterClass; |
import org.junit.BeforeClass; |
import org.junit.Test; |
import com.fengke.ReadNumbers; |
public class Task1 { |
@BeforeClass |
public static void setUpBeforeClass() throws Exception { |
System.out.println( "Task1开始执行" ); |
} |
@AfterClass |
public static void tearDownAfterClass() throws Exception { |
System.out.println( "Task1执行完毕" ); |
} |
@Test |
public void testMax() { |
assertEquals( 2 , new ReadNumbers( 2 , 2 ).max()); |
} |
} |
============================= |
package com.test; |
import static org.junit.Assert.*; |
import org.junit.AfterClass; |
import org.junit.BeforeClass; |
import org.junit.Test; |
import com.fengke.ReadNumbers; |
public class Task2 { |
@BeforeClass |
public static void setUpBeforeClass() throws Exception { |
System.out.println( "Task2开始执行" ); |
} |
@AfterClass |
public static void tearDownAfterClass() throws Exception { |
System.out.println( "Task2执行完毕" ); |
} |
@Test |
public void testMax() { |
assertEquals( 3 , new ReadNumbers( 3 , 2 ).max()); |
} |
} |
====================================== |
package com.test; |
import static org.junit.Assert.*; |
import org.junit.AfterClass; |
import org.junit.BeforeClass; |
import org.junit.Test; |
import com.fengke.ReadNumbers; |
public class Task3 { |
@BeforeClass |
public static void setUpBeforeClass() throws Exception { |
System.out.println( "Task3开始执行" ); |
} |
@AfterClass |
public static void tearDownAfterClass() throws Exception { |
System.out.println( "Task3执行完毕" ); |
} |
@Test |
public void testMax() { |
assertEquals( 4 , new ReadNumbers( 4 , 2 ).max()); |
} |
} |
======================================================= |