上游充通大学 SHANGHAI JIAO TONG UNIVERSITY Eclipses会询问是否添加junit jar包,确定后新建一个HelloTest类, 用来测试Hello类。一般自己导入下载的junit包。 New JUnit Test Case ☒ JUnit Test Case Select the name of the new JUnit test case.You have the options to specify the class under test and on the next page,to select methods to be tested. Source Folder: Hello/src Browse,· Package: example Browse.· Name: HelloTest Superclass: junit.framework.TestCase Browse.· Which method stubs would you like to create? public static void main(String[]args) Add TestRunner statement For: text ui setup() tearDown( 厂 constructor( Class Under Test: example.Hello Browse... <Back Next> Finish Cancel
Eclipse会询问是否添加junit.jar包,确定后新建一个HelloTest类, 用来测试Hello类。一般自己导入下载的junit包
上游究通大学 SHANGHAI JIAO TONG UNIVERSITY 选中setUp0和tearDown(),然后点击“Next': Newy JUnit Test Case ☒ Test Methods Select methods for which test method stubs should be created. Available methods: □c。Hello Select All s main(String[]) ☑o abs(int) Deselect All ☐Object 1 method selected. Create final method stubs Create tasks for generated test methods Back Next Cancel
选中setUp()和tearDown(),然后点击“Next”:
上游充通大学 SHANGHAI JIAO TONG UNIVERSITY 选择要测试的方法,我们选中abs(int)方法,完成后在 HelloTest,java中输入: 1 package example: 2 import junit.framework.TestCase: 3 4vpublic class HelloTest extends TestCase 5 private Hellohello: 6 79 protected void setUp()throws Exception 8 super.setUp(): 9 hello new Hello(); 10 11 △12 protected void tearDown()throws Exception 13 super.tearDown() 14 15 16 public void testAbs() 17 a33 ertEqua1s(he11o.abs〔14),14): 18 assertEquals(hello.abs(-5),5): 19 assertEquals(hello.abs(0), 0): 20 21
选择要测试的方法,我们选中abs(int)方法,完成后在 HelloTest.java中输入: