This article will introduce you to JUnit, a tool for testing and debugging in the project. 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. Explain 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 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. Establish 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.
The code for TesttHatwegetHelloWorldPROMPT is as follows:
Import junit.framework.testcase;
Public Class TestttathWegetHelloWorldPrompt
Extends testcase {
Public TesttathWegetHelloWorldPrompt (
String name) {
Super (Name);
}
Public void testsay () {
HelloWorld Hi = New HelloWorld ();
Assertequals ("Hello World!", Hi.SAY ());
}
Public static void main (String [] args) {
Junit.textui.teStrunner.run
TesttathWegetHelloWorldprompt.class;
}
}
The code is not complicated; it is 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.).
TesttHatwegetHelloWorldPROMPT.JAVA includes these: Testcase subclats and a method called testsay (). This method calls the assertequals () function, which is used to compare our expected values and the values returned by Say ().
The main () method is used to run test and display output. JUnit's Testrunner handling test provides an image and text-based output performance form. We use text-based versions because Eclipse supports it, and it is also suitable for us. When you start running, text-based version tests are output in text, and Eclipse automatically turn these outputs into the output of the image interface. According to the TDD specification, the test is first run, and it should be deliberately let it fail. Click Run -> Run to -> JUnit Test (Remember TestttHatwegetHelloWorldPrpt.java should be prominent in the package resource manager). On the left window, you should see the JUnit window instead of the package resource manager, it shows a red strip, a failed test, and the specific failure is taken in Figure 2. If you do not automatically display these content, click the JUnit tab (on the left side of the bottom).
Figure 2. Testing of the failure in JUnit
well! But it failed. Now let's establish the test code: right click on the projectwithjunit header of the package resource manager window, select New -> class. Choose a class name, we have assumed it called HelloWorld, then click Finish. Fill in the following code for HelloWorld.java:
Public class helloworld {
Public string say () {
Return ("Hello World!");
}
}
This code is very simple, even don't need an annotation, let's take a look at the results. According to the manner described above, a green strip is displayed in the JUnit window, referring to Figure 3. Green strips have proven to be successful.
Figure 3. Successful test in JUnit
Now, we want to make the test failed once, but the reason is different. This helps to display different error information in the JUnit test. Modify the assertequals () code to turn "Hello World!" Into "Hello ME!". When running JUnit again, the result turned into a red bar, and the cause of failure at the bottom of the JUnit window, referring to Figure 4.
Figure 4. ComparisonError in JUnit
Finally, I want to say about the topic of the necessary part of the development process. Test code has always been an important part of development. After recent years, it has been greatly improved, which is due to strong theoretical research (such as "Expectations-Based Development", and rapidly developing test kits, and testing process. If you are interested in this article, please spend some time to formally learn the test theory, this is useful for your work.
About the Author:
Alexander Prohorenko A UNIX system administrator, network security administrator.
Olexiy Prohorenko A Java developer lived in DnieProptrovsk in Ukraine.