官术网_书友最值得收藏!

Unit-testing frameworks

In this section, two of the most used Java frameworks for unit testing are shown and briefly commented on. We will focus on their syntax and main features by comparing a test class written using both JUnit and TestNG. Although there are slight differences, both frameworks offer the most commonly used functionalities, and the main difference is how tests are executed and organized.

Let's start with a question. What is a test? How can we define it?

A test is a repeatable process or method that verifies the correct behavior of a tested target in a determined situation with a determined input expecting a predefined output or interactions.

In the programming approach, there are several types of tests depending on their scope—functional tests, acceptance tests, and unit tests. Further on, we will explore each of those types of tests in more detail.

Unit testing is about testing small pieces of code. Let's see how to test a single Java class. The class is quite simple, but enough for our interest:

public class Friendships { 
  private final Map<String, List<String>> friendships = 
new HashMap<>(); public void makeFriends(String person1, String person2) { addFriend(person1, person2); addFriend(person2, person1); } public List<String> getFriendsList(String person) { if (!friendships.containsKey(person)) { return Collections.emptyList(); } return friendships.get(person) } public boolean areFriends(String person1, String person2) { return friendships.containsKey(person1) && friendships.get(person1).contains(person2); } private void addFriend(String person, String friend) { if (!friendships.containsKey(person)) { friendships.put(person, new ArrayList<String>()); } List<String> friends = friendships.get(person); if (!friends.contains(friend)) { friends.add(friend); } } }
主站蜘蛛池模板: 栖霞市| 巨鹿县| 特克斯县| 石棉县| 永吉县| 南华县| 武川县| 资兴市| 重庆市| 屏东县| 外汇| 陈巴尔虎旗| 大方县| 涞水县| 石河子市| 疏勒县| 宜兰县| 刚察县| SHOW| 澄城县| 靖远县| 湖口县| 新乡县| 讷河市| 阿拉尔市| 大悟县| 阜新| 东源县| 卓资县| 六盘水市| 招远市| 特克斯县| 成武县| 娄烦县| 青海省| 寻甸| 清河县| 西畴县| 石家庄市| 璧山县| 通榆县|