文章标题
来源:互联网 发布:简明python教程3.0 编辑:程序博客网 时间:2024/05/29 16:05
JUnit4使用Java5中的注解(annotation),以下是JUnit4常用的几个annotation:
@Before:初始化方法 对于每一个测试方法都要执行一次(注意与BeforeClass区别,后者是对于所有方法执行一次)
@After:释放资源 对于每一个测试方法都要执行一次(注意与AfterClass区别,后者是对于所有方法执行一次)
@Test:测试方法,在这里可以测试期望异常和超时时间
@Test(expected=ArithmeticException.class)检查被测方法是否抛出ArithmeticException异常
@Ignore:忽略的测试方法
@BeforeClass:针对所有测试,只执行一次,且必须为static void
@AfterClass:针对所有测试,只执行一次,且必须为static void
一个JUnit4的单元测试用例执行顺序为:
@BeforeClass -> @Before -> @Test -> @After -> @AfterClass;
每一个测试方法的调用顺序为:
@Before -> @Test -> @After;
- public class JUnit4Test {
- @Before
- public void before() {
- System.out.println(”@Before”);
- }
- @Test
- /**
- *Mark your test cases with @Test annotations.
- *You don’t need to prefix your test cases with “test”.
- *tested class does not need to extend from “TestCase” class.
- */
- 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”);
- };
- };
public class JUnit4Test { @Before public void before() { System.out.println("@Before"); } @Test /** *Mark your test cases with @Test annotations. *You don’t need to prefix your test cases with “test”. *tested class does not need to extend from “TestCase” class. */ 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
@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 的方法抛出异常的的情况下也一样会。
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 一个简单的dubbo项目搭建案例
- CentOS定时执行shell脚本配置
- Less.Html 示例一:获取嵌套元素中的正确内容
- 为服务器生成证书
- 远程控制项目
- 文章标题
- apicloud开发之教程
- win10手动安装英语语言包
- 设计师如何明确设计目标?
- Windows下Nginx配置SSL实现Https访问(包含证书生成) Windows下Nginx配置SSL实现Https访问(包含证书生成) 首先要说明为什么要实现https? HTT
- 388. Longest Absolute File Path
- SQL Server存储过程中编写事务处理的方法小结
- 如何减轻拔牙的疼痛?研究人员建议用VR观看沿海场景
- 练习