[关闭]
@Yano 2016-03-23T14:36:59.000000Z 字数 3464 阅读 2254

JUnit Tutorial

Java


关于 JUnit 的两篇文章,搬运过来了:

http://www.programcreek.com/2012/02/junit-tutorial-eclipse/
http://www.programcreek.com/2012/02/junit-tutorial-2-annotations/

Introduction and Installation

JUnit is the defacto standard for unit testing.

JUnit is part of Eclipse Java Development Tools (JDT). So, we can either install JDT via Software Updates site, or download and install Eclipse IDE for Java Developers.

Using JUnit in Eclipse Environment

1. Create a project and create a class.

This should contains the method you want to test.

  1. public class MyString {
  2. public static String capitalize(String str) {
  3. int strLen;
  4. if (str == null || (strLen = str.length()) == 0) {
  5. return str;
  6. }
  7. return new StringBuilder(strLen)
  8. .append(Character.toTitleCase(str.charAt(0)))
  9. .append(str.substring(1))
  10. .toString();
  11. }
  12. }

2. Create a test case by using JUnit Wizard.

Right Click the class -> new -> JUnit Test Case

Complete the steps.

Fill up the following code to the test case:

  1. public class MyStringTest {
  2. @Test
  3. public void testMyStringCapitalize(){
  4. assertEquals(null, MyString.capitalize(null));
  5. //similarily we can use assertTrue()
  6. assertTrue(null == MyString.capitalize(null));
  7. assertEquals("capitalize(empty-string) failed", "", MyString.capitalize("") );
  8. }
  9. }

3. Run the test case as JUnit Test

4. Use code coverage tool to check if all statements/branches are covered in your test case(s).

For example, Clover, Emma.

We can easily see that some part of the method is not covered, which means test case is not good enough.

You may ask questions like: How to skip a test case in Junit? How to timeout a test? etc. Annotations can make those very simple to implement, without even writing any complex code for configuration.

@Test

This marks a method to be a test method. A test class can have MULTIPLE test methods.

@Before and @After

Will execute the method before/after each test. This method can prepare/clean up the test environment (e.g. read input data, initialize the class, delete temporary data, etc). As a class can have multiple test methods, @Before and @After methods will be executed before and after each test. This is different with @BeforeClass and @AfterClass.

  1. @Before
  2. public void setUp() throws Exception {
  3. //setup something
  4. }
  5. @After
  6. public void tearDown() throws Exception {
  7. //tear down something
  8. }

@BeforeClass and @AfterClass

@BeforeClass executes before the start of tests. This can be used to perform time intensive activities, e.g., connect to a database. @AfterClass executes after all tests have finished. This can be used to perform clean-up activities, e.g., disconnect from a database. Those two only run one time no matter how many test the class has. Also you have to declare “@BeforeClass” and“@AfterClass” method as static method. The two annotations should be used do some static initialization code and destroy static variables.

  1. @BeforeClass
  2. public static void runBeforeClass() {
  3. // run before all test cases
  4. }
  5. @AfterClass
  6. public static void runAfterClass() {
  7. // run after all test cases
  8. }

@Ignore

Will ignore the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included.

  1. @Ignore("ignored method")
  2. @Test
  3. public void someTestMethod() {
  4. System.out.println("Method is ignored");
  5. }

@Test (expected = Exception.class)

If a test case expects some exception, use this annotation. It fails, if the method does not throw the named exception.

  1. @Test(expected = ArithmeticException.class)
  2. public void divisionWithException() {
  3. int i = 1/0;
  4. }

@Test(timeout=100)

If you want to time out a test, use timeout annotation. It will fail, if the method takes longer than 100 milliseconds.

  1. @Test(timeout = 100)
  2. public void timeoutMethod() {
  3. while (true);
  4. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注