JBuilderx uses JUnit learning notes 1. Introduction
Since we have had a general understanding of Junit, I hope to provide you with a slightly official manual to write JUnit test documents, understand some of the key terms and concepts. But what I want to declare is that this is not a full manual, I can only think that it is a new entry manual. Software with other OpenSource has the same problem, JUnit's documentation has the rules, concise and completeness of the commercial software document. The document written by developers is always not clear, and the whole document needs to refer to the "Official" guide, API manual, email discussion group email, even in the source code and related comments. In fact, the problem is not so complicated unless you have very special requirements, otherwise, you can get the required information.
2. installation
JUnit is integrated in JBuilderX, you can use JUnit, not just the configuration under JBuilderx, you can use it.
3. Fixed process
JUnit defines a fixed format to establish a test case:
1. IMPLEMENT a subclass of Testcase.
2. Define a public instance variable in the stored state.
3, initialize public variables by Overriding Setup.
4. Clear initialization after testing through Overriding Teardown.
5. Create a test method for TestXxxxxx ().
6. Once you have created some test instances, in order to let them run together. A TestSuite must be defined.
Through such a frame, it can easily develop the corresponding test class.
4. Write test cases Testcase
JUnit provides two ways to define in the TestCase class. Protected void setup () throws java.lang.exception protected void teardown () THROWS JAVA.LANG.EXCEPTION
Overwrite Setup () methods, initialize all tests of FixTure (you can even create a network connection in Setup), which is slightly different from each test in the Testxxxx () method.
Cover Teardown (), release your permanent resource allocated in Setup (), such as database connections.
When the JUnit performs a test, it calls setup () before performing each TestXxx () method, and call the Teardown () method after performing each Test Testxxxx () method, thereby ensuring that the test does not affect each other.
Need to remind, define considerable Assert methods in the JUnit.Framework.assert class, mainly assert (), assert (), assertequals (), assertnull (), assertsame (), asserttrue (), asserttrue (), fail (), etc. method. If you need to compare your own defined classes, such as Car. The Assert method requires you to override the Equals () method of the Object class to compare the difference between the two objects. Practice shows: If you override the equals () method of the Object class, it is best to override the HashCode () method of the Object class. Further, the toString () method of the Object class is also covered. This makes the test result more readable.
Be sure to ensure the PUBLIC property of the TestXxx () method, otherwise the test cannot be called.
5. Write a collection test Testsuit
Define a static suite () method in the TestCase class. The suite () method is like the Main () method, JUnit uses it to perform tests. In the suite () method, you add a test instance to a TestSuite object and return to this TestSuite object. A TestSuite object can run a set of tests. TestSuite and TestCase have implemented Test Interfaces, while the Test interface defines the methods required to run tests. This allows you to create a TestSuite with a combination of TestCase and TestSuite. Junit offers three interfaces to run test
[Text Ui] junit.textui.teStrunner
[AWT UI] junit.awtui.teStrunner
[Swing ui] junit.swingui.teStrunner
JBuilder9 running unit test:
1. Build a project and add a class, such as JunitStudy.java.
2. Choose the Project | Rebuild Project "junitstudy.jpx" command, compile the project, so that you can make the class file in the project in the Test Case wizard.
3. Double-click the JUnitStudy.java file in the engineering window to open in the code window.
4. Select the Test tab from the Object Gallery dialog, select the Test Case icon from the TEST tab, and then click the OK button, which junitstudy.java file will be displayed as the Test Class Wizard.
5. Select a method in the TEST CASE wizard. Click the finish button, such a new Test Case, Test TestJunitStudy.java file is opened at the code window.
6. After adding the corresponding code, click the save button in the main toolbar to save the project.
7. Right-click the TestjunitStudy.java file and select the run test class from the pop-up shortcut menu to start running this test class, and developers can see its operation in the message window.
8. If the operation schedule is red, it means that this class has an error.
<< - Create a Test Suit - >>
9. Add a new test method in TestJunitStudy.java and save
10. Select the file | new command to pop up the Object Gallery dialog. Select the Test Suit Icon, click the OK button to pop up the Test Suit Wizard.
11. Select TestJunitstudy in the wizard as a test case contained in Testsuit.
12. Click Next to modify ClassName to AllTests.java.
13. Then click Finish so AllTests.java is added to the developer's project.
14. Right-click the allteests.java file in the engineering window and select the Run Test Using Alltests command from the pop-up shorttests to start running the test class.
references:
1. Application Junit implement unit testing (original author: eric)
2. JUnit implementation (Java developer) http://www.neweasier.com/
3. JUnit study notes (bamboo shoots fried meat)