软件测试- 2 - 简单的带Annotation和不带Annotation的Junit测试

SUT(System Under Test)

public class Op {

	public int add(int x, int y) {
		return x + y;
	}

}


不带Annotation的Junit测试

import junit.framework.TestCase;

public class OpJunit3Test extends TestCase {

	int x, y;

	@Override
	public void setUp() throws Exception {
		x = 1;
		y = 4;
	}

	@Override
	public void tearDown() throws Exception {
	}

	public void testAdd() {
		Op op = new Op();
		int z = op.add(x, y);
		assertEquals(5, z);
	}

}


带Annotation的Junit测试

属于Junit4的特性,要能够使用它必须先下载JUnit-4的jar包,下载地址:http://www.junit.org/
下载完以后再Eclipse IDE中添加到Build Path中即可。

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class OpJunit4Test{
	
	int x,y;
	
	@Before
	public void before() throws Exception {
		x=1;y=4;
	}

	@After
	public void after() throws Exception {
	}
	
	@Test
	public void add(){
		Op op = new Op();
		int z = op.add(x, y);
		assertEquals(5, z);
	}
}


为了解释Junit和它的Annotation的使用,我参见了教程:http://www.vogella.de/articles/JUnit/article.html

以下两张参考表来自该教程

3.3. Assert statements

The following gives an overview of the available test methods:

Table 2. Test methods

Statement Description
fail(String) Let the method fail, might be usable to check that a certain part of the code is not reached.
assertTrue(true); True
assertsEquals([String message], expected, actual) Test if the values are the same. Note: for arrays the reference is checked not the content of the arrays
assertsEquals([String message], expected, actual, tolerance) Usage for float and double; the tolerance are the number of decimals which must be the same
assertNull([message], object) Checks if the object is null
assertNotNull([message], object) Check if the object is not null
assertSame([String], expected, actual) Check if both variables refer to the same object
assertNotSame([String], expected, actual) Check that both variables refer not to the same object
assertTrue([message], boolean condition) Check if the boolean condition is true.

3.2. Annotations

The following give an overview of the available annotations in JUnit 4.x

Table 1. Annotations

Annotation Description
@Test public void method() Annotation @Test identifies that this method is a test method.
@Before public void method() Will perform the method() before each test. This method can prepare the test environment, e.g. read input data, initialize the class)
@After public void method() Test method must start with test
@BeforeClass public void method() Will perform the method before the start of all tests. This can be used to perform time intensive activities for example be used to connect to a database
@AfterClass public void method() Will perform the method after all tests have finished. This can be used to perform clean-up activities for example be used to disconnect to a database
@Ignore Will ignore the test method, e.g. useful if the underlying code has been changed and the test has not yet been adapted or if the runtime of this test is just to long to be included.
@Test(expected=IllegalArgumentException.class) Tests if the method throws the named exception
@Test(timeout=100) Fails if the method takes longer then 100 milliseconds

在学习Annotation这一部分是,对@Before和@BeforeClass的区别,我还是不能体会。

所以,我参考了这一篇博文:http://dongwei.iteye.com/blog/143836

以下表格,来自于该博文:

@BeforeClass and @AfterClass@Before and @After
在一个类中只可以出现一次

在一个类中可以出现多次,即可以在多个方法的声明前加上这两个Annotaion标签,执行顺序不确定

方法名不做限制方法名不做限制
在类中只运行一次在每个测试方法之前或者之后都会运行一次

@BeforeClass父类中标识了该Annotation的方法将会先于当前类中标识了该Annotation的方法执行。
@AfterClass 父类中标识了该Annotation的方法将会在当前类中标识了该Annotation的方法之后执行

@Before父类中标识了该Annotation的方法将会先于当前类中标识了该Annotation的方法执行。
 @After父类中标识了该Annotation的方法将会在当前类中标识了该Annotation的方法之后执行
必须声明为public static必须声明为public 并且非static
所有标识为@AfterClass的方法都一定会被执行,即使在标识为@BeforeClass的方法抛出异常的的情况下也一样会。所有标识为@After 的方法都一定会被执行,即使在标识为 @Before 或者 @Test 的方法抛出异常的的情况下也一样会。

@BeforeClass 和 @AfterClass 对于那些比较“昂贵”的资源的分配或者释放来说是很有效的,因为他们只会在类中被执行一次。相比之下对于那些需要在每次运行之前都要初始化或者在运行之后都需要被清理的资源来说使用@Before和@After同样是一个比较明智的选择。


那么带Annotation和不带Annotation的JUnit测试的区别在那里呢?

不带Annotation的Junit必须:

1.继承自TestCase

2.测试方法必须命名为testXXX()


带Annotation的Junit必须:

1.带Annotation的函数必须为public



我们的课程里有那么一道题:

用JUnit可以测试一个方法是否正确抛出异常,针对使用Annotation和不用Annotation,各举一个示例说明具体的步骤。


那么我们来看一下怎么回答

不用Annotation测试一个方法是否正确抛出异常

我们用fail表示我们没有捕捉到我们想捕捉的异常

import java.io.File;

import junit.framework.TestCase;

public class OpJunit3Test extends TestCase {

	public void testAdd() {
		try{
			String path = null;
			File file = new File(path);
			fail("Created File from null! It's undefined");
		}catch (NullPointerException e) {
			assertEquals(NullPointerException.class, e.getClass());
		}
	}

}

用Annotation测试一个方法是否正确抛出异常

没有捕捉到的异常会自动被Junit认为是一个错误

import java.io.File;
import org.junit.Test;

public class OpJunit4Test{
	
	@Test(expected=NullPointerException.class)
	public void add(){
		String path = null;
		File file = new File(path);
	}
}

附加的学习:

Junit4与Junit3有什么主要的区别:

来自于:http://032615.iteye.com/category/174477?show_full=true

1.2 Junit4与Junit3的主要区别

  1.2.1 Junit4引入了java 5.0的注释技术:

  这两个版本最大的区别在JUnit3.x中测试必须继承 TestCase,并且每个方法名必须以test开头。比如:testMethod1()而在JUnit4.x中不必继承TestCase,采用了注解的方式。只要在测试的方法上加上注解@Test即可,从而不必再遵循以前的一些显式约定和反射定位测试;在JUnit4.x中如果继承了TestCase,注解就不起作用了。并且有很重要的一点就是在JUnit4.x中继承了TestCase后,在OutLine视图中测试单个方法时,结果整个类都run 了。还有一点就是,在3.x中需要实现setUp和tearDown方法,而在4.x中无需这样,可以自定义需要在测试前和测试后的方法,在方法前加上 @before,@after就可以了。所以在JUnit4.x不必继承TestCase用注解即可对单个方法进行测试。

  1.2.2 JUnit4引入了一个JUnit3中没有的新特性——类范围的 setUp() 和tearDown() 方法。

  任何用 @BeforeClass 注释的方法都将在该类中的测试方法运行之前刚好运行一次,而任何用 @AfterClass 注释的方法都将在该类中的所有测试都运行之后刚好运行一次。

  1.2.3 异常测试:

  异常测试是Junit4中的最大改进。Junit3的异常测试是在抛出异常的代码中放入try块,然后在try块的末尾加入一个fail()语句。

  例如该方法测试一个被零除抛出一个ArithmeticException:

  该方法不仅难看,而且试图挑战代码覆盖工具,因为不管测试是否通过还是失败,总有一些代码不被执行。在JUni4中,可以编写抛出异常的代码,并使用注释来声明该异常是预期的:

  如果没有异常抛出或者抛出一个不同的异常,那么测试就将失败。

  1.2.4 JUnit4添加了两个比较数组的assert() 方法:

  public static void assertEquals(Object[] expected, Object[] actual)

  public static void assertEquals(String message, Object[] expected, Object[] actual)

  这两个方法以最直接的方式比较数组:如果数组长度相同,且每个对应的元素相同,则两个数组相等,否则不相等。数组为空的情况也作了考虑。






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值