Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222392
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: Java

2011-03-30 10:48:07

How do I write and run a simple test?

  1. Create a class:



    package junitfaq;

    import org.junit.*;
    import static org.junit.Assert.*;

    import java.util.*;

    public class SimpleTest {
  2. Write a test method (annotated with @Test) that asserts expected results on the object under test:



    @Test
    public void testEmptyCollection() {
    Collection collection = new ArrayList();
    assertTrue(collection.isEmpty());
    }
  3. If you are running your JUnit 4 tests with a JUnit 3.x runner, write a suite() method that uses the JUnit4TestAdapter class to create a suite containing all of your test methods:



    public static junit.framework.Test suite() {
    return new junit.framework.JUnit4TestAdapter(SimpleTest.class);
    }
  4. Although writing a main() method to run the test is much less important with the advent of IDE runners, it's still possible:



    public static void main(String args[]) {
    org.junit.runner.JUnitCore.main("junitfaq.SimpleTest");
    }
    }
  5. Run the test:

    • To run the test from the console, type:

      java org.junit.runner.JUnitCore junitfaq.SimpleTest
    • To run the test with the test runner used in main(), type:

      java junitfaq.SimpleTest

    The passing test results in the following textual output:


    .
    Time: 0

    OK (1 tests)
阅读(469) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~