例子,写一个公共方法
public class JunitTest { public int plus(int a, int b) { return a + b; } }
然后,右键点击这个原文件-》工具-》创建Junit测试
之后会让你选择是junit3还是junit4,我这选了4,接着下一个对话框都默认
之后会在测试包 生成一个JunitTestTest.java(名字起得不好。。。)
public class JunitTestTest { public JunitTestTest() { } @BeforeClass public static void setUpClass() throws Exception { } @AfterClass public static void tearDownClass() throws Exception { } @Before public void setUp() { } @After public void tearDown() { } /** * Test of plus method, of class JunitTest. */ @Test public void testPlus() { System.out.println("plus"); int a = 0; int b = 0; JunitTest instance = new JunitTest(); int expResult = 0; int result = instance.plus(a, b); assertEquals(expResult, result); // TODO review the generated test code and remove the default call to fail. fail("The test case is a prototype."); } }
最后对public void testPlus() 进行修改,把fail去掉
@Test public void testPlus() { System.out.println("plus"); int a = 1; int b = 2; JunitTest instance = new JunitTest(); int expResult = 3; int result = instance.plus(a, b); assertEquals(expResult, result); // TODO review the generated test code and remove the default call to fail. }
最后ctrl+F6运行~~~ the end