eclipse JUnit Unit test
Install JUnit
- Download junit-4.10.jar from http://sourceforge.net/projects/junit/files/junit/4.10/ to workspace/your-project/lib/
- start eclipse
- select your-project top node
- right click
- select [Refresh]
- select your-project top node
- right click
- select [Property]
- select [Java Build Path]
- select [Libraries] tab
- click [add JARs]
- select lib/junit-4.10.jar
- click [OK]
generate unit-test
- select your class(want test)
- right click
- [New] -> [JUnit test case]
- [Finish]
run unit-test
- right click generated (edited) unit-test class
- [run as] -> [JUnit Test]
unit-test example
test this class
public class ChangeNum { public static void main(String[] args) { ChangeNum cn1 = new ChangeNum(); cn1.changeNum(args[0]); } int changeNum(String s1) { int n1 = Integer.parseInt(s1); return n1; } }
- select this class
- right click
- [New] -> [JUnit test case]
- [Finish]
- edit generated file as:
import junit.framework.TestCase; public class ChangeNumTest extends TestCase { public ChangeNumTest(String name) { super(name); } public void testChangeNum() { String ts1 = "100"; int tn1 = 100; ChangeNum tcn1 = new ChangeNum(); int test1 = tcn1.changeNum(ts1); assertEquals("Error not number type", tn1, test1); } }
- right click ChangeNumTest class
- [run as] -> [JUnit Test]