JUnit 4 使用 Java 5 中的注解(annotation) 【转】
来源:互联网 发布:大数据需要解决的问题 编辑:程序博客网 时间:2024/05/16 19:52
http://niatwangcong.iteye.com/blog/292820
JUnit 4 使用 Java 5 中的注解(annotation),以下是JUnit 4 常用的几个 annotation 介绍
@Before:初始化方法
@After:释放资源
@Test:测试方法,在这里可以测试期望异常和超时时间
@Ignore:忽略的测试方法
@BeforeClass:针对所有测试,只执行一次,且必须为static void
@AfterClass:针对所有测试,只执行一次,且必须为static void
一个JUnit 4 的单元测试用例执行顺序为:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一个测试方法的调用顺序为:
@Before –> @Test –> @After
写个例子测试一下,测试一下
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 JUnit4Test {
@Before
public void before() {
System.out.println("@Before");
}
@Test
public void test() {
System.out.println("@Test");
assertEquals(5 + 5, 10);
}
@Ignore
@Test
public void testIgnore() {
System.out.println("@Ignore");
}
@Test(timeout = 50)
public void testTimeout() {
System.out.println("@Test(timeout = 50)");
assertEquals(5 + 5, 10);
}
@Test(expected = ArithmeticException.class)
public void testExpected() {
System.out.println("@Test(expected = Exception.class)");
throw new ArithmeticException();
}
@After
public void after() {
System.out.println("@After");
}
@BeforeClass
public static void beforeClass() {
System.out.println("@BeforeClass");
};
@AfterClass
public static void afterClass() {
System.out.println("@AfterClass");
};
};
输出结果
@BeforeClass
@Before
@Test(timeout = 50)
@After
@Before
@Test(expected = Exception.class)
@After
@Before
@Test
@After
@AfterClass
eclipse运行结果:
<wbr></wbr><wbr></wbr><wbr>
可以看到testIgnore是被忽略的,没有执行
- JUnit 4 使用 Java 5 中的注解(annotation) 【转】
- JUnit 4 使用 Java 5 中的注解(annotation)
- JUnit 4 使用 Java 5 中的注解(annotation)
- JUnit 4 使用 Java 5 中的注解(annotation)
- JUnit 4 使用 Java 5 中的注解(annotation)
- JUnit中的注解(Annotation)
- Java中的注解(@ Annotation)
- JAVA中的注解(Annotation)
- java中的注解(Annotation)
- java中的注解Annotation
- Java中的Annotation注解
- java Annotation(注解)使用
- java中的内置注解Annotation
- Java中的注解(annotation)
- Java注解Annotation(转)
- Java中的Annotation详解和使用 自定义注解
- Annotation注解,参考MultiType中的使用注解
- Java 注解Annotation使用解析
- 发送邮件
- 修改oracle用户密码
- 如何调试makefile变量
- 动态规划之——装配线调度
- 关于豆瓣网网页版体验心得
- JUnit 4 使用 Java 5 中的注解(annotation) 【转】
- 双击VC对话框提示cannot add new member的解决方法
- qt实现无标题栏的可移动窗体
- 使用INTERVAL DAY TO SECOND类型
- html中使用javascript调用本地程序(exe,doc等)
- vc实现窗口全屏的一些方法
- 处理9path图片边缘的小黑点
- HDU 3691 Nubulsa Expo(SW算法求最小割)
- xcode P M # T C etc