Junit @ Eclipse 2

xiaoxiao2021-03-06  50

Eclipse Ver 3 can be created directly in JUnit Winzard, but you need to configure before use in Ver 2, which is described in detail in "Eclipse In Action". The following is the original text, plus simple guidance! First, first create a work set A Sample Application and Working Sets

Second, create a class variables: JUNIT join the project file First you define the class variables using the Package Explorer as follows: 1 Right-click on the project name and select Properties from the contextmenu.2 In the Properties dialog that appears, select Java Build Path in the rightpane and select the Libraries tab.3 On this page, click the Add variable button.4 On the next page, click New. Enter JUNIT for the variable name and clickthe File button to browse for the JUnit JAR file under the Eclipse Pluginsdirectory; this May be, for example, c: /eclipse/plugins/org.junit_3.8.1/junit.jar.5 Click Open to select the jar from the file dialog box, and then click Okto Accept the new variable.6 Next . you'll add a variable for the source JAR for JUnit, in case you needit for debugging Click New again, and this time enter JUNIT_SRC as thename Click File and locate the junitsrc.zip under the JDT source directory; eg C:. / Eclipse / plugins / org.eclipse.jdt.source_2.1.0 / src / org.junit_3.8.1 / junitsrc.zip.7 Click Ok to Retur n to the new variable classpath entry box.

Third, create a class variables: JUNIT_SRC and JUNIT association (reason: the need for source code debugging) Now you'll add the JUNIT variable to your classpath and associate the source JAR with it, using the JUNIT_SRC variable: 1 Click on the JUNIT classpath variable and click OK.2 Make sure you are on the Java Build Path page in the Properties dialogbox, and click the plus sign next to the JUNIT entry. you will see thatthere is no Javadoc and no source attached.3 Double-click on Source Attachment and enter the variable name JUNIT_SRC Click OK and verify that the source JAR (for example, c: /eclipse/plugins/org.junit_3.8.1/src.jar). is now attached.4 Click OK to save the classpath changes and Dismiss The Properties Dialogbox.note That A Junit Library is now listed in the package ixplorer. if you open the library (by clicking the sign), you can expens the contents of the library.

Fourth, the use JUnit Winzard create a test case classesThe easiest way to create test case classes is to use the JUnit wizard: 1 Right-click on the file for which you want to create test cases-FilePersistenceServices-and select New → Other from the context menu .2 Notice that in the New dialog box, you can expand the Java selection onthe left by clicking the plus sign. Doing so reveals a selection for JUnit.3 Select JUnit on the left to present the choices TestCase and TestSuite onthe right.4 Select TestCase (see figure 3.1). Click Next.5 in the box that follows, accept the default values ​​provided for the folder, package, test case, test class, and superclass. Later, especially for largerprojects, you may consider putting tests in their own package, but keepingunit tests in the same package as the code they test has the advantageof giving them access to methods that have package access.6 in addition to the default test entries, click the options to create methodstubs for setUp () and tearDown ( (See Figure 3.2) ). Click Next.7 In the next dialog box, you are presented with the option to create methodstubs to test each of the methods in the FilePersistenceServices classand its superclass Object. Check the boxes for the FilePersistenceServicesread () and write () methods (see Figure 3.3). (if you do '() Methods, You Probably Didn't Save The FilePersistenceServices Class After ADDING. Click Cancel and try again. 8 Click Finish.

You're finally ready to add some tests JUnit's primary tools for testing are a variety of overloaded assert methods (public void assertXXXX) for testing an expression or pair of expressions.These include the following:. ■ assertEquals (x, y) -Test Passes if x and y Are Equal. x AND y haus an appropriate equals () method. ■ Assertfalse (b) -test passes if Boolean value b is false. ■ asserttrue (b) -test passes if Boolean Value B Is True. ■ AssertNull (o) -Test Passs IF Object O is Null. ■ AssertNotnull (O) -Test Passs IF Object O Is Not Null. ■ Assertsame (Ox, Oy) -Test Passs if Ox and Oy Refer To The Same Object. ■ AssertNotsame (OX, OY) -Test Passs if Ox and Oy Do Not Refer to the SameObject. 5. Run SELECT RUN → Run As → JUnit Test.

6. Creating a new method in Test Case Classes, generating a method in the corresponding class with Quick Fix Tool! 1 Click ON One of the Light Bulbs.2 Double-Click On The Suggested Fix: Create YourmethodName

转载请注明原文地址:https://www.9cbs.com/read-82985.html

New Post(0)