Simplify your Java tests with Ruby

Source: Internet
Author: User
Tags count stringbuffer

Martin Fowler: Of course (willing to spend half the time to write unit tests)! Because unit testing enables you to get your work done faster. Countless practices have proved this. The more stressful your time is, the more you have to write unit tests, which look slow, but actually help you achieve your goals faster and more comfortably.

Unit testing is important, but ...

The importance of unit testing, I would like to do a little more emphasis is not too much. But the reality is that I often hear Java developers complaining that unit tests are tedious and difficult to write. Although reluctantly for it, but the busy, and did not realize the benefits of it! The resulting result is a large number of unit tests that can only be run once. is to simply attribute responsibility to developers? Or is the development process or system imperfect?

To be fair, when I'm doing TDD or unit tests myself, there are times when I do get bored, especially when it comes to preparing test data or testing environments, for example, we often need to randomly generate strings for a certain length for testing, and we need the following code:

public String getRandomAlphabetic(int count) {
       count = count <= 0 ? 5 : count; //默认为5
       //构建一个包含所有英文字母的字符串
       String alphabet="abcdefghijklmnopqistuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
       StringBuffer sb = new StringBuffer(count);
       for (int i = 0; i < count; i++) {
         int character=(int)(Math.random()*26);
         sb.append(alphabet.substring(character, character+1));
       }
       return sb.toString();
   }

If you use Ruby,

def random_alphabetic(size=5)
       chars = ('a'..'z').to_a + ('A'..'Z').to_a # 构建一个从a到Z的一个字母表数组
      (0...size).collect { chars[rand(chars.length)] }.join # 从chars数组中返回指定长度的随机字符数组(默认5个),调用join方法,将这个数组中的所有元素连接成一个字符串
   end

How do you feel when you compare? Experienced developers will soon be challenged to say that we have a ready-made Commons-lang library, a simple call to Randomstringutils.randomalphabetic (5) To complete the task, but I would like to ask if there is no Third-party library support, Which way would you prefer? You can also imagine building a tree-like structure of data, Ruby's Way

data =<<-EOF
   {
   "order_id": "xxx-xxxxx-xxx",
   "books": [
     {"ISBN": "2323-2323", "number": 2, "price": 20.00},
     {"ISBN": "2323-2324", "number": 3, "price": 30.00},
     {"ISBN": "2323-2325", "number": 2, "price": 20.00},
     {"ISBN": "2323-2326", "number": 3, "price": 30.00},
     {"ISBN": "2323-2327", "number": 2, "price": 20.00}
    ]
   }
   EOF     # 该数据为json格式的一段字符串
   order = JSON.parse(data)
   p order['books'][0]['ISBN'] #=> 2323-2323

How do you do it in Java, where many people will sacrifice the first "XML" of the seven most abused weapons in the Java world, even if it is so elegant? If it wasn't for a simple "language cult", at least I would not hesitate to choose to do it in Ruby's way. Save a little time, leave work early to accompany wife also good! :)

Related Article

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.