一 几个常用注释
@Before 被它注释的方法,在每个测试方法运行之前都要执行
@After 与@Before相对,被它注释的方法,在每个测试方法运行之后执行
上述两个注释,它们是方法级别的,因为每个测试方法的运行前后,都会触发它们的执行。因此,有影响效率的嫌疑
这两类方法,都用public void 修饰,而且不能带有任何参数,命名随便
view plaincopy to clipboardprint?
@Before
public void setBeforeMethod()
{
}
@After
public void seAfterMethod()
{
}
@Before
public void setBeforeMethod()
{
}
@After
public void seAfterMethod()
{
}
@BeforeClass 被它注释的方法,在该类中的测试方法运行之前刚好运行一次
@AfterClass 被它注释的方法,都将在该类中的所有测试都运行之后刚好运行一次
上述两个注释,是类级别的,对一个类来说,就执行一次
这两类方法,都用public static void 修饰,而且不能带有任何参数,命名随便
view plaincopy to clipboardprint?
@BeforeClass
public static void setBeforeClass()
{
}
@AfterClass
public static void setAfterClass()
{
}
@BeforeClass
public static void setBeforeClass()
{
}
@AfterClass
public static void setAfterClass()
{
}
上面的四个注释,用于:为测试方法的执行,做一些准备工作和销毁工作。
@Test 被它注释的方法,就是测试方法
@Test(expected=XXXException.class) 异常测试,若该异常XXXException没有抛出,或抛出另一个异常,则测试失败
@Test(timeout=500) 可以帮助进行性能测试,为方法运行设置时间限制,单位毫秒,过时未完成则认为失败
测试方法,都用public void 修饰,而且不能带有任何参数,命名随便
view plaincopy to clipboardprint?
@Test
public void testToString1()
{
Example1 e = new Example1();
Assert.assertEquals("snoopy:0", e.toString());
}
@Test(expected = java.lang.ArithmeticException.class)
public void test()
{
int i = 0;
int j = 2;
int r = j / i;
}
@Test(timeout = 2)
public void test()
{
int r = 1;
for(int i = 0; i < 100; i ++ )
{
r *= 2;
}
}
@Test
public void testToString1()
{
Example1 e = new Example1();
Assert.assertEquals("snoopy:0", e.toString());
}
@Test(expected = java.lang.ArithmeticException.class)
public void test()
{
int i = 0;
int j = 2;
int r = j / i;
}
@Test(timeout = 2)
public void test()
{
int r = 1;
for(int i = 0; i < 100; i ++ )
{
r *= 2;
}
}
@Ignore 跳过该方法,不执行(忽略,只是权宜之计,如果写了一个方法,一直忽略,那就要考虑干嘛写它了)
view plaincopy to clipboardprint?
// @Ignore
@Ignore(".. because of ...")
@Test
public void testToString1()
{
Example1 e = new Example1();
Assert.assertEquals("snoopy:0", e.toString());
}
二 私有方法的单元测试
public方法的测试,相对要容易些,获取对象实例,调用public方法,就可以了。
protected方法的测试,可以通过建立一个与被测类同包名的测试类来解决。
private方法的测试,稍微麻烦些。
私有方法的测试,一般是采用发射的原理进行。
可以自己手动编写这一反射过程,如:
view plaincopy to clipboardprint?
package application;
//待测试类
public class Example1
{
public void print(int i, int j)
{
System.out.println(sum(i, j));
}
private int sum(int i, int j)
{
return i + j;
}
}
package application;
//待测试类
public class Example1
{
public void print(int i, int j)
{
System.out.println(sum(i, j));
}
private int sum(int i, int j)
{
return i + j;
}
}
手动编写利用反射特性,完成对私有方法的测试:
view plaincopy to clipboardprint?
package application;
import java.lang.reflect.Method;
import junit.framework.Assert;
import org.junit.Test;
public class Example1Test
{
@Test
public void testSum()
{
try
{
Class clazz = Class.forName("application.Example1");
Example1 instance = (Example1) clazz.newInstance();
Class [] args = new Class []{ int.class, int.class };
Method sum = instance.getClass().getDeclaredMethod("sum", args);
sum.setAccessible(true);
Assert.assertEquals(2, sum.invoke(instance, 1, 1));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
package application;
import java.lang.reflect.Method;
import junit.framework.Assert;
import org.junit.Test;
public class Example1Test
{
@Test
public void testSum()
{
try
{
Class clazz = Class.forName("application.Example1");
Example1 instance = (Example1) clazz.newInstance();
Class [] args = new Class []{ int.class, int.class };
Method sum = instance.getClass().getDeclaredMethod("sum", args);
sum.setAccessible(true);
Assert.assertEquals(2, sum.invoke(instance, 1, 1));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
除了自己手动编写外,我在网上找到了一个,可以帮助我们完成私有方法测试的小框架,其底层原理还是反射:
提供该framework的网站,包括下载、简介之类的信息 http://code.google.com/p/accessive/
view plaincopy to clipboardprint?
package application;
//待测试的类
public class Example2
{
private int age;
private String name;
public Example2(int age, String name)
{
this.age = age;
this.name = name;
}
private String tostring()
{
return tostring(this.age, this.name);
}
private String tostring(int age, String name)
{
return name + " : " + age;
}
}
package application;
//待测试的类
public class Example2
{
private int age;
private String name;
public Example2(int age, String name)
{
this.age = age;
this.name = name;
}
private String tostring()
{
return tostring(this.age, this.name);
}
private String tostring(int age, String name)
{
return name + " : " + age;
}
}
使用accessive小框架
view plaincopy to clipboardprint?
package application;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import com.j2speed.accessor.FieldAccessor;
import com.j2speed.accessor.MethodAccessor;
public class Example2Test
{
private static Example2 instance;
@BeforeClass
public static void init()
{
instance = new Example2(12, "Sufie");
}
// 访问私有变量
@Test
public void testField()
{
FieldAccessor < Example2, Integer > ageInt =