Big talk rebuilding serialization 14: We automated testing like this, refactoring 14

Source: Internet
Author: User
Tags date now

Big talk rebuilding serialization 14: We automated testing like this, refactoring 14

After talking about this, let's take an example to see how to perform automated testing for system refactoring. Return to the previous HelloWorld example (for details, see "3.3 little steps"). There is a sayHello () method in this class, as long as we enter the current time and user name, returns the greeting to this user. If the current time is in the morning, "Hi, XXX. Good morning!" is returned !"; In the afternoon, "Hi, XXX. Good afternoon!" is returned !"; If it is Night, "Hi, XXX. Good Night!" is returned !", This is the function implemented by the HelloWorld program.

Then we start to write the test code for this program (if the test-driven development is used, we should first write the test code and then write the program ). We first create a test source program directory, and then build the package and test program corresponding to the program under test. This means that if the program under test is in the "org. refactoring. helloWorld. in the resource package, the test program should establish "test.org. refactoring. helloWorld. the resource package corresponds to it. If the program to be tested is called "HelloWorld", a "HelloWorldTest" class is created to correspond to it. This class is a JUnit test program.

The following is how to write this test program to execute the test. Since the tested program has three branches, namely, the current time is morning, afternoon, and evening, we have created three test cases for them respectively. The test procedure is as follows:

/*** Test for {@ link org. refactoring. helloWorld. resource. helloWorld} * @ author fangang */public class HelloWorldTest {private HelloWorld helloWorld = null;/*** @ throws java. lang. exception */@ Beforepublic void setUp () throws Exception {helloWorld = new HelloWorld ();}/*** @ throws java. lang. exception */@ Afterpublic void tearDown () throws Exception {helloWorld = null;}/*** Test method for {@ link or G... helloWorld # sayHello (java. util. date, java. lang. string )}. * // @ Testpublic void testSayHelloInTheMorning () {Date now = DateUtil. createDate (2013, 9, 7, 9, 23, 11); String user = "Bao Xiaomei"; String result = ""; result = helloWorld. sayHello (now, user); assertThat (result, is ("Hi, Bao Xiaomei. good morning! ");}/*** Test method for {@ link org... helloWorld # sayHello (java. util. date, java. lang. string )}. * // @ Testpublic void testSayHelloInTheAfternoon () {Date now = DateUtil. createDate (2013, 9, 7, 15, 7, 10); String user = "off"; String result = ""; result = helloWorld. sayHello (now, user); assertThat (result, is ("Hi, close two pots. good afternoon! ");}/*** Test method for {@ link org... helloWorld # sayHello (java. util. date, java. lang. string )}. * // @ Testpublic void testSayHelloAtNight () {Date now = DateUtil. createDate (2013, 9, 7, 21, 30, 10); String user = "IT siege Lions"; String result = ""; result = helloWorld. sayHello (now, user); assertThat (result, is ("Hi, IT siege lion. good night! "));}}


This program is written in JUnit4, where assertThat (result, is ("Hi, IT siege lion. Good night! "), The first parameter is the result of the execution of the program under test, and the second parameter is verified based on the expected results. If the execution result is the same as the expected result, the test passes. Otherwise, the test fails.

Then we run the test program and get the following results:


Figure 4.1 JUnit test results

 

All three test cases passed. The test is successful!

Now we have compiled test cases for the original program and passed all tests. All the preparations for refactoring are ready. Then, we start the first refactoring. As described above, we adjusted the program sequence for the first refactoring, segmented the program, added comments, and modified the corresponding variables to make it easier to read. This is a small step, and it takes only three or five minutes to complete the reconstruction. When the Refactoring is completed and the program returns to the compiled running state, we execute this test program and the test passes. Passing the test means that, although the code inside the program has been modified, the program's external functions remain unchanged. That is, if the program's external behavior does not change, the reconstruction is successful and we can continue the subsequent work.

In the second refactoring, we use the "Extraction Method" to extract the getFirstGreeting (), getSecondGreeting (), and getHour () methods from the sayHello () function. Then we run the test program again and the test passes.

In the third refactoring, we use the "extraction class" to extract getFirstGreeting () and getSecondGreeting () to form GreetingToUser and GreetingAboutTime respectively. After the test is completed, the test is passed.

In the fourth reconstruction, our needs have changed. Greetings not only change with the morning, afternoon, and evening of the day, but also determine whether the greeting is a holiday based on different dates. In this case, we adopt the "two hats" Method for development: first, we do not introduce new requirements, but only modify the original program to adapt it to new requirements. To this end, we extract DateUtil from the GreetingAboutTime class so that not only getHour (), but also getMonth () and getDate () are available (). After reconstruction, the test passes.

The design of "two hats" is another difference in system reconstruction. We will discuss it in detail later. Then we began to add new requirements to write getGreeting () in GreetingAboutTime as follows:

/** * @return the greeting about time */public String getGreeting(){DateUtil dateUtil = new DateUtil(date);int month = dateUtil.getMonth();int day = dateUtil.getDay();int hour = dateUtil.getHour();if(month==1 && day==1) return "Happy new year! ";if(month==1 && day==14) return "Happy valentine's day! ";if(month==3 && day==8) return "Happy women's day! ";if(month==5 && day==1) return "Happy Labor day! ";......if(hour>=6 && hour<12) return "Good morning!";if(hour==12) return "Good noon! ";if(hour>=12 && hour<19) return "Good afternoon! ";if(hour>=19 && hour<22) return "Good evening! ";return "Good night! ";}


Then our test fails:


Figure 4.2 test cases fail

 

Why cannot I pass the testSayHelloAtNight test? After carefully checking the program under test, we find that its function has changed to: if the current time is July 22, January 1, "Hi, XXX. Happy new year!" is returned !"; If it is July 22, January 14, "Hi, XXX. Happyvalentine's day!" is returned !"...... If the current time is not these holidays, if it is morning, return "Hi, XXX. Good morning !", At noon, "Hi, XXX. Good noon!" is returned !", In the afternoon, "Hi, XXX. Good afternoon!" is returned !", In the evening, "Hi, XXX. Good evening!" is returned !", Otherwise, "Hi, XXX. Good night!" is returned !". Because of this, we need to adjust our test programs and write test cases for each branch. After the test is modified, the test passes.

Big talk rebuilding serialization home: http://blog.csdn.net/mooodo/article/details/32083021

Note: I hope that the author or source should be noted to show my respect for the author when I reprint this article. Thank you!





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.