Use junit in Eclipse (translation)

xiaoxiao2021-03-05  28

Use in Eclipse JUnit (translated) Author: rosen's Blog This article will introduce you to Junit, in a project for testing and debugging tools. After introducing TDD (in test drive development) theory, further explain how to build your own JUnit test in popular Eclipse. To you show you how to test the simple program like Hello World. Automated testing is discussed in many books, but only rare books have noticed such a problem, however, how to organize these tests. As the test increases, placement and calling these tests become more troublesome. This will become an important issue, so that the TDD appears, the limit programming (XP) has enabled TDD. In addition, you can understand the TDD: Developed by testing. The main specification of TDD: Before writing program code, the corresponding automatic test must be written. Even the program code does not exist, it also sees a failure test result. After the test passes, the copy code must be discarded. There is a specific step (which may refer to "Extreme Programming") can be referred to by any programmer without a special other method. These steps (chapters) should be first read before we start writing tests - how to organize automatic testing. Explain different types of tests: unit test: The correctness of the detection module (that is, class). If the object needs to access the external data resource, such as a database, you need to simulate a Mock Objects, but in the actual real data and test environments are different. Customer test: This is a functional, system, and acceptance test. Used to test the overall system characteristics. In XP, these tests are written by the user. Comprehensive test: Bridge between user testing and unit testing. Comprehensive test helps test the interactivity of the application. Under normal circumstances, Mock Objects is not used for comprehensive testing, which will increase test time. Similarly, comprehensive tests often rely on special test environments, such as database delivery test data. Comprehensive tests also need to use external class libraries. For example, a class library CACTUS for a comprehensive test for J2EE applications. Interpret these tests beyond the scope of this article, please refer to http://jakarta.apache.org/cactus/. Developer Test: This is used to let developers check their own code or new functions. For each developer, there is a need to have more tests to verify the code. Organize these tests as important as the organizational code. In the following chapters, as long as the "test" is mentioned, then the developer test is referred to. We are almost ready to start establishing tests, first should choose a name for our test. You may say, "This is not a problem: put the word 'Test' in front of the class name, just!" Will n't so fast! Let me talk about the problem of this step: In TDD, the class or method of being tested does not exist. A test can overwrite multiple methods, or even multiple classes, which is possible. The above is just some universal problems; there are more problems. Let me take a suggestion, when the test is named: The name of the test class should make people know that this is a test class, and can explain what it wants to test, pay attention to whether or not the other class. According to the above recommendations, it is very simple, nor is it too long or difficult to listen. We will create our first test with the JUnit tool in Eclipse. Suppose you have downloaded a newest Eclipse version. If you haven't yet, you should go to the official site http://www.eclipse.org download.

JUnit is also required, or you can download it from http://www.junit.org/. Run Eclipse. Create a new Workplace project, click File -> New -> Project, select the Java project, click Next. Get a project name, such as ProjectWithjunit. Click to complete. This is the establishment of the new project. Continue with Eclipse, add the JUnit class library to the build path. Click on the item-> property on the toolbar, select the Java build path, library, select Add External JAR, browse JUnit stored directories, select JUnit.jar, click Open. You will see that JUnit appears in the list of libraries. Click OK to let the Eclipse rebate the path. Now develop our "Hello World" example. According to the rules of TDD, you should write the test before the code is established. In order to be able to start a certain start, we assume that the future class name is HelloWorld, and there is a method Say (), which returns the value of String (for example, "Hello World!"). Establish a test, click Right click on the topical title of ProjectWithjunit, select New-> Other, expand the "Java" option, select JUnit. Select the test case in the column dialog on the right, then the next step. Refer to Figure 1. Figure 1. Establishing a JUnit Test in Eclipse In the column of the test class, write the class name helloworld that will be tested. Choose a name of a test case, such as TesttHatwegetHelloWorldPrompt (Yes, it looks very long, but it is very clear that its behavior.) Click to complete. TestThatWeGetHelloWorldPrompt code is as follows: import junit.framework.TestCase; public class TestThatWeGetHelloWorldPrompt extends TestCase {public TestThatWeGetHelloWorldPrompt (String name) {super (name);} public void testSay () {HelloWorld hi = new HelloWorld (); assertEquals (/ "Hello World! / ", Hi.say ()); public static void main (string [] args) {junit.textui.teestrunner.run (TesttHatwegetHelloWorldprompt.class);}} The code is not complicated; just a bit different. However, let's examine the details. We inherited the JUnit's TestCase class, which defined JUnit's Javadocs as "junit, which runs many tests." JUnit also has a TestSuite class, which is a set of test cases, but not discussions in this article. The steps to establish a test case are as follows: 1. Establish an instance of junit.framework.testcase. 2. Define some non-return methods that start with "TEST" (such as TestWastransAnSuccessful (), TestShow (), etc.).

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

New Post(0)