`

java Junit

    博客分类:
  • Java
 
阅读更多

 

 

什么是单元测试

写了个类,要给别人用,会不会有bug?怎么办?测试一下。

main方法测试好不好?不好!

1.         不能一起运行!

2.         大多数情况下需要人为的观察输出确定是否正确

为什么要进行单元测试

重用测试,应付将来的实现的变化。

提高士气,明确知道我的东西是没问题的。

JUnit4 HelloWorld

1.         new project

2.         建立类

3.         建立testcase

放弃旧的断言,使用hamcrest断言

1.         assertThat

2.         使用hamcrest的匹配方法

a)         更自然

3.         示例

a)         assertThat( n, allOf( greaterThan(1), lessThan(15) ) );
assertThat( n, anyOf( greaterThan(16), lessThan(8) ) );
assertThat( n, anything() );
assertThat( str, is( "bjsxt" ) );
assertThat( str, not( "bjxxt" ) );

b)        assertThat( str, containsString( "bjsxt" ) );
assertThat( str, endsWith("bjsxt" ) ); 
assertThat( str, startsWith( "bjsxt" ) ); 
assertThat( n, equalTo( nExpected ) ); 
assertThat( str, equalToIgnoringCase( "bjsxt" ) ); 
assertThat( str, equalToIgnoringWhiteSpace( "bjsxt" ) );

c)         assertThat( d, closeTo( 3.0, 0.3 ) );
assertThat( d, greaterThan(3.0) );
assertThat( d, lessThan (10.0) );
assertThat( d, greaterThanOrEqualTo (5.0) );
assertThat( d, lessThanOrEqualTo (16.0) );

d)        assertThat( map, hasEntry( "bjsxt", "bjsxt" ) );
assertThat( iterable, hasItem ( "bjsxt" ) );
assertThat( map, hasKey ( "bjsxt" ) );
assertThat( map, hasValue ( "bjsxt" ) );

FailureError

1.         Failure是指测试失败

2.         Error是指测试程序本身出错

JUnit4 Annotation

1.         @Test: 测试方法

a)         (expected=XXException.class)

b)        (timeout=xxx)

2.         @Ignore: 被忽略的测试方法

3.         @Before: 每一个测试方法之前运行

4.         @After: 每一个测试方法之后运行

5.         @BeforeClass: 所有测试开始之前运行(对应的方法必须是static)

6.         @AfterClass: 所有测试结束之后运行

运行多个测试

注意

1.         遵守约定,比如:

a)         类放在test包中

b)        类名用XXXTest结尾

c)         方法用testMethod命名

其他框架

TestNG

 

 

 

使用Junit4

一、简介
    Junit 4是Junit框架有史以来的最大改进,其主要目标便是利用Java 5的Annotation特性简化测试用例的编写。
    在Eclipse 3.2中已经自带了Junit 4.1(我使用的开发环境是Eclipse 3.3+Myeclipse 6.0),我们可以将Junit 4 Library添加到项目用到的Library中。也可以到Junit.org去下载Junit 4 Librar。要求:JDK 5.0或以上版本。


二、使用
1、Junit 3中,Junit依赖反射来执行每个以test开头的方法,但是Junit 4中,有了Annotation的,我们的测试方法就不需要再以testXXX来标识了,而是写上一个@Test标注即可。如:
@Test public static void m1() {}

2、测试类也不必继承自TestCase了。我们可能会想到,不继承TestCase,我们就无法使用断言,即无法调用assertXxx()方法了。所以,在Junit 4中,所有的断言的方法全部都以静态方法的形式放入到了Assert类中,使用Assert.assertXxx()来调用,如果使用import static静态导入Assert,那么与Junit 3中使用断言是一样的。

3、Junit 3中,setUp()和tearDown()方法分别是准备测试环境和释放资源,在Junit 4中,这二个方法依赖于@Before和@After标记,好处是如果我们忘记了在这二个方法中调用父类的同名方法,Junit框架会自动处理使用@Before和@After标记的方法。

4、不再强迫必须使用setUp()和tearDown()作为方法名,可以使用更有意义的方法名。如:init()和close(),前提是它们被标注了@Before和@After标记


三、实例开发
我们还使用讲解Junit 3时使用的Calculate类

package junit;

public class Calculate {
public int add(int a, int b) {
   return a + b;
}

public int minus(int a,int b)
{
   return a - b;
}
}

 

Junit 4测试类


public class CalculateTest {
private Calculate cal ;

public CalculateTest()
{
   System.out.println("创建了CalculateTest类的实例");
}

@Before
public void init() throws Exception {
   cal = new Calculate();
   System.out.println("测试方法前调用@Befored");
}

@After
public void close() throws Exception {
   System.out.println("测试方法后调用@After");
}

@Test
public void doAdd() {
   assertEquals(cal.add(1, 2), 3);
}

@Test
public void testMinus() {
   assertEquals(cal.minus(2, 1), 1);
}
}


四、测试异常:
对测试异常,JUnit 4可以用expected=Exception.class来期待一个预期的异常,而不必手动编写。如:测试以下方法,


public int div(int a,int b)
{
   return a / b;
}


可能会抛出除数为0的异常,我们可以这样来编写:


@Test(expected=ArithmeticException.class)
public void testDiv() {
   cal.div(5, 0);
}


五、设置最长执行时间
对于非常耗时的测试,@Test还有一个timeout来标识该方法最长执行时间,超过此时间即表示该方法测试失败:1为毫秒数


@Test(timeout=1)
public void testMinus() {
   assertEquals(cal.minus(2, 1), 1);
}


六、@BeforeClass和@AfterClass
    这是Junit 4与Junit 3相比较另一个较大的区别。它们在一个Test类的所有测试方法执行前后各执行一次。这是为了初始化一些重要的资源,比如数据库连接,会在@BeforeClass中来执行初始化,然后再执行测试方法,最后在@AfterClass中释放资源。
    由于这二个方法在运行其间仅执行一次,因此它们只能标记为静态方法,其实在所有的测试方法中共享的资源也必须是静态引用。如:


@BeforeClass
public static void setUpBeforeClass() throws Exception {
     System.out.println("call @BeforeClass and init database connection");
     dbConnection = new Object();
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
     System.out.println("call @AfterClass to release database connection");
     dbConnection = null;
}


注意:
Java 5.0中的自动Box/UnBox特性,在调用assertEquals()时要注意:如果我们这样写:
assertEquals(100f,100);
按照自动装箱会变成:
assertEquals(new Float(100f),new Integer(100));
这样会引起测试失败,因为它们类型不一样。
因此我们对float和double的测试,应试使用:
assertEquals(float, float, float delta);
assertEquals(double, double, double delta);
还有一点,Junit 4不能与以前的版本兼容。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics