- 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); } } }
- Bootstrap Site Blueprints Volume II
- Microsoft Application Virtualization Cookbook
- C語言程序設計(第2版)
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- The DevOps 2.4 Toolkit
- Apache Mesos Essentials
- 單片機應用與調試項目教程(C語言版)
- R Data Analysis Cookbook(Second Edition)
- Scala程序員面試算法寶典
- Advanced Express Web Application Development
- Learning AngularJS for .NET Developers
- Creating Data Stories with Tableau Public
- 自學Python:編程基礎、科學計算及數據分析(第2版)
- SQL Server 入門很輕松(微課超值版)
- Python大規模機器學習