- Test-Driven Java Development(Second Edition)
- Alex Garcia Viktor Farcic
- 248字
- 2021-06-24 18:31:47
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?
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); } } }
- Magento 2 Theme Design(Second Edition)
- Django:Web Development with Python
- Python高級編程
- C語言程序設計立體化案例教程
- Java Web應用開發技術與案例教程(第2版)
- HTML5+CSS3網頁設計
- MongoDB權威指南(第3版)
- HTML5從入門到精通(第4版)
- 響應式Web設計:HTML5和CSS3實戰(第2版)
- 后臺開發:核心技術與應用實踐
- Qlik Sense? Cookbook
- Python Natural Language Processing
- Python網絡運維自動化
- Go Programming Blueprints
- 架構師應該知道的37件事