有关Junit和多线程测试的问题
如果你想验证下列代码,你需要下载并安装Junit。按着指示去做,以便能够在Junit的网站能够找到它。不要过分追求细节,我们将简要的介绍Junit是怎样工作的。要写一个Junit的测试,你必须首先创建一个扩展于junit.framework.TestCase(Juint中的基本测试类)的测试类。
Main()方法和suite()方法被用启动测试。无论是从命令行还是IDE集成开发环境窗口,必须确保junit.jar在你的CLASSPATH环境变量里指定。然后为BadExampleTest.Class类编译运行下列代码:
import junit.framework.*; public class BadExampleTest extends TestCase { // For now, just verify that the test runs public void testExampleThread() throws Throwable { System.out.println("Hello, World"); } public static void main (String[] args) { String[] name = { BadExampleTest.class.getName() }; junit.textui.TestRunner.main(name); } public static Test suite() { return new TestSuite( BadExampleTest.class); } }
import junit.framework.*; public class BadExampleTest extends TestCase { private Runnable runnable; public class DelayedHello implements Runnable { private int count; private Thread worker; private DelayedHello(int count) { this.count = count; worker = new Thread(this); worker.start(); } public void run() { try { Thread.sleep(count); System.out.println( "Delayed Hello World"); } catch(InterruptedException e) { e.printStackTrace(); } } } public void testExampleThread() throws Throwable { System.out.println("Hello, World"); //1 runnable = new DelayedHello(5000); //2 System.out.println("Goodbye, World"); //3 } public static void main (String[] args) { String[] name = { BadExampleTest.class.getName() }; junit.textui.TestRunner.main(name); } public static Test suite() { return new TestSuite( BadExampleTest.class); } }
| 第1页: 介绍 | 第2页: 有关Junit和多线程测试的问题 |
| 第3页: 进入GroboUtils | 第4页: 编写多线程测试 |