`
niwtsew
  • 浏览: 70804 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JUnit4 in 60 Seconds

阅读更多

copy from http://www.cavdar.net/2008/07/21/junit-4-in-60-seconds/

 

played with JUnit 4 library this weekend and here is the short introduction to it:

  1. @Test
    Mark your test cases with @Test annotations. You don’t need to prefix your test cases with “test”.  In addition, your class does not need to extend from “TestCase” class. (My note: 如果你的class不幸继承了TestCase的话,那么还将沿用Junit3的那一套寻找测试函数的模式(即函数名test***)@Test 这个annotattion将无效。

 

    @Test  
    public void addition() {  
        assertEquals(12, simpleMath.add(7, 5));  
    }  
      
    @Test  
    public void subtraction() {  
       assertEquals(9, simpleMath.substract(12, 3));  
    }  

 2. @Before and @After

  1. Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case.
    @Before
    public void runBeforeEveryTest() {
    simpleMath = new SimpleMath();
    }

    @After
    public void runAfterEveryTest() {
    simpleMath = null;
    }
     
  2. @BeforeClass and @AfterClass
    Use @BeforeClass and @AfterClass annotations for class wide “setup” and “tearDown” respectively. Think them as one time setup and tearDown. They run for one time before and after all test cases.
        @BeforeClass  
        public static void runBeforeClass() {  
            // run for one time before all test cases  
        }  
          
        @AfterClass  
        public static void runAfterClass() {  
            // run for one time after all test cases  
        }  
     
  3. Exception Handling
    Use “expected” paramater with @Test annotation for test cases that expect exception. Write the class name of the exception that will be thrown.
        @Test(expected = ArithmeticException.class)  
        public void divisionWithException() {  
            // divide by zero  
            simpleMath.divide(1, 0);  
       }  
     
  4. @Ignore
    Put @Ignore annotation for test cases you want to ignore. You can add a string parameter that defines the reason of ignorance if you want.
        @Ignore("Not Ready to Run")  
        @Test  
        public void multiplication() {  
            assertEquals(15, simpleMath.multiply(3, 5));  
        }  
     
  5. Timeout
    Define a timeout period in miliseconds with “timeout” parameter. The test fails when the timeout period exceeds.
        @Test(timeout = 1000)  
        public void infinity() {  
            while (true)  
                ;  
        }  
     
  6. New Assertions
    Compare arrays with new assertion methods. Two arrays are equal if they have the same length and each element is equal to the corresponding element in the other array; otherwise, they’re not.

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

     @Test  
     public void listEquality() {  
         List<Integer> expected = new ArrayList<Integer>();  
         expected.add(5);  
       
         List<Integer> actual = new ArrayList<Integer>();  
         actual.add(5);  
       
         assertEquals(expected, actual);  
     } 
     
  7. JUnit4Adapter
    Run your Junit 4 tests in Junit 3 test runners with Junit4Adapter.
        public static junit.framework.Test suite() {  
            return new JUnit4TestAdapter(SimpleMathTest.class);  
       }  
     

Happy coding. :D

 

 

My Note goes here:

文中没有提到JUnit4中TestSuite的用法,新的用法TestSuite.addTest(..)已经不再适用,因为新的testclass都没有extends TestCase.取而代之的用法如下:

import junit.framework.Test;
import junit.framework.TestSuite;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({YouTestCaseOne.class,YourTestCase2.class})
public class AllTests {

}
分享到:
评论

相关推荐

    JUnit in Action 3nd Edition

    "JUnit in Action 3rd Edition" JUnit是一种流行的Java单元测试框架,由Kent Beck和Eric Gamma于1997年创立。JUnit在软件测试领域中扮演着重要的角色,帮助开发者编写高质量的代码。下面是关于JUnit的重要知识点: ...

    junit4测试jar包

    JUnit4测试框架是Java开发中广泛使用的单元测试工具,它为开发者提供了编写和运行可重复、可靠的测试用例的能力。这个“junit4测试jar包”包含了一切你需要在项目中集成JUnit4进行测试的库文件。只需将其复制到你的...

    junit4 jar完整包

    JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了一种方便、高效的方式来验证代码的正确性。这个“junit4 jar完整包”包含了所有你需要进行单元测试的类和接口,使得测试过程变得简单且易于...

    junit4 jar包

    JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了编写可重复执行、易于维护的测试代码的能力。这个“junit4 jar包”包含了运行JUnit4测试所必需的库文件,主要包括两个核心组件:`junit-4.11....

    junit4学习文档

    ### JUnit4 学习知识点详解 #### 一、JUnit4 概述 JUnit4 是 JUnit 测试框架的一个重大更新版本,它充分利用了 Java 5 的注解(Annotation)特性来简化测试用例的编写过程。注解是一种元数据,用于描述程序中的...

    JUnit4JUnit4JUnit4(文档)

    JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了强大的工具来编写、组织和执行单元测试。JUnit4引入了许多改进和新特性,极大地提升了测试的灵活性和效率。下面将详细介绍JUnit4的关键概念、...

    junit4教程(《Junit4初探》)

    **JUnit4教程——初探单元测试的艺术** JUnit4是Java编程语言中广泛使用的单元测试框架,它是Java开发者进行软件质量保证的重要工具。本教程将深入浅出地介绍JUnit4的基本概念、核心特性以及如何在实际项目中应用它...

    JUnit in Action(JUnit经典书籍)中文+英文

    《JUnit in Action》是一本专为Java开发人员深入理解单元测试框架JUnit而编写的经典书籍。本书详尽地探讨了如何有效地使用JUnit进行软件测试,包括基础概念、高级特性和最佳实践,旨在帮助读者提升软件质量,降低...

    Junit4教程非常详尽

    JUnit4 教程详尽 JUnit4 是 JUnit 框架有史以来的最大改进,其主要目标便是利用 Java5 的 Annotation 特性简化测试用例的编写。下面是对 JUnit4 的详细介绍: 一、Annotation 简介 Annotation 是 Java5 中引入的...

    JUnit in Action中文版

    JUnit in Action中文版JUnit in Action中文版JUnit in Action中文版JUnit in Action中文版

    JUnit4基础文档

    JUnit4基础文档 单元测试是软件测试的一种,旨在检验软件的正确性和可靠性。JUnit是一个流行的单元测试框架,广泛应用于Java开发中。本文档介绍了JUnit4的基础知识,包括单元测试的概念、JUnit4的HelloWorld示例、...

    powermock-module-junit4-2.0.9-API文档-中英对照版.zip

    赠送jar包:powermock-module-junit4-2.0.9.jar; 赠送原API文档:powermock-module-junit4-2.0.9-javadoc.jar; 赠送源代码:powermock-module-junit4-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-...

    JUnit in action JUnit Recipies

    《JUnit in Action》和《JUnit Recipes》是两本关于Java单元测试的重要书籍,它们深入浅出地介绍了如何使用JUnit框架进行高效、可靠的测试。JUnit是一个流行的开源测试框架,广泛用于Java应用程序的单元测试,它提供...

    Junit4完整源码

    JUnit4 是一个广泛使用的Java编程语言的单元测试框架。它为开发者提供了一种方便的方式来编写和执行可重复的、自动化的测试用例,确保代码的质量和功能稳定性。JUnit4源码的完整版本包含了整个框架的实现细节,对于...

    JUnit in Action 中文版电子书

    书中还详细讲解了JUnit 4和JUnit 5的区别,让读者了解最新版本带来的改进和新特性。 此外,《JUnit in Action 中文版》还讨论了测试的组织结构,包括测试套件的管理,以及如何通过使用注解来定制测试行为。读者将...

    Junit4.zip

    《JUnit4:Java单元测试框架详解》 JUnit4是一款广泛应用于Java编程领域的单元测试框架,它的出现极大地简化了测试代码的编写,提升了测试的效率。本文将深入探讨JUnit4的核心特性、使用方法以及如何将其应用到实际...

    junit in action 源码

    7. **测试规则**:JUnit4中的TestRule接口和JUnit5中的Extension API提供了扩展测试行为的能力,如日志记录、性能测试等。通过源码,我们可以学习如何自定义测试规则。 8. **异步测试**:随着并发编程的普及,JUnit...

    JUnit_in_Action,_2nd_Edition.pdf

    ### JUnit in Action, 2nd Edition - 关键知识点概览 #### 一、书籍简介与价值 《JUnit in Action, 2nd Edition》是一本深入介绍JUnit框架的专业书籍,适用于希望提高单元测试技能和软件质量意识的程序员。本书由...

Global site tag (gtag.js) - Google Analytics