JBuilder2005单元测试之业务类介绍_MySQL
JBuilder
为了便于讲解,拟通过两个简单的业务类引出测试用例,一个是分段函数类,另一个是字符串处理类,在这节里我们先来熟悉这两个业务类。
分段函数类
分段函数Subsection类有两个函数,sign()是一个符号函数,而getValue(int d)函数功能如下:
当d
当-2≤d
当d=0时,值为100;
当2≤d时,值为d*d*d。
其代码如下图所示:
代码清单 错误!文档中没有指定样式的文字。分段函数
1. package chapter25;
2.
3. public class Subsection
4. {
5. public static int getValue(int d) {
6. if (d == 0) {
7. return 100;
8. } else if (d 9. return Math.abs(d);
10. } else if (d >= -2 && d 11. return d * d;
12. } else { //d >= 2
13. // if (d > 32) {
14. // return Integer.MAX_VALUE;
15. // }
16. return d * d * d;
17. }
18. }
19.
20. public static int sign(double d) {
21. if (d 22. return -1;
23. } else if (d > 0) {
24. return 1;
25. } else {
26. return 0;
27. }
28. }
29. }
在getValue()方法中,当d>32时,d*d*d的值将超过int数据类型的最大值(32768),所以当d>32时,理应做特殊的处理,这里我们特意将这个特殊处理的代码注释掉(第13~15行),模拟一个潜在的Bug。
字符串处理类
由于标准JDK中所提供的String类对字符串操作功能有限,而字符串处理是非常常用的操作,所以一般的系统都提供了一个自己的字符串处理类。下面就是一个字符串处理类,为了简单,我们仅提供了一个将字符串转换成数组的方法string2Array(),其代码如下所示:
代码清单 错误!文档中没有指定样式的文字。字符串处理类
1. package chapter25;
2. public class StringUtils
3. {
4. public static String[] string2Array(String str, char splitChar, boolean trim) {
5. if (str == null) {
6. return null;
7. } else {
8. String tempStr = str;
9. int arraySize = 0; //数组大小
10. String[] resultArr = null;
11. if (trim) { //如果需要删除头尾多余的分隔符
12. tempStr = trim(str, splitChar);
13. }
14. arraySize = getCharCount(tempStr, splitChar) + 1;
15. resultArr = new String[arraySize];
16. int fromIndex = 0, endIndex = 0;
17. for (int i = 0; i 18. endIndex = tempStr.indexOf(splitChar, fromIndex);
19. if (endIndex == -1) {
20. resultArr[i] = tempStr.substring(fromIndex);
21. break;
22. }
23. resultArr[i] = tempStr.substring(fromIndex, endIndex);
24. fromIndex = endIndex + 1;
25. }
26. return resultArr;
27. }
28. }
29.
30. //将字符串前面和后面的多余分隔符去除掉。
31. private static String trim(String str, char splitChar) {
32. int beginIndex = 0, endIndex = str.length();
33. for (int i = 0; i 34. if (str.charAt(i) != splitChar) {
35. beginIndex = i;
36. break;
37. }
38. }
39. for (int i = str.length(); i > 0; i--) {
40. if (str.charAt(i - 1) != splitChar) {
41. endIndex = i;
42. break;
43. }
44. }
45. return str.substring(beginIndex, endIndex);
46. }
47.
48. //计算字符串中分隔符中个数
49. private static int getCharCount(String str, char splitChar) {
50. int count = 0;
51. for (int i = 0; i 52. if (str.charAt(i) == splitChar) {
53. count++;
54. }
55. }
56. return count;
57. }
58. }
除对外API string2Array()外,类中还包含了两个支持方法。trim()负责将字符前导和尾部的多余分隔符删除掉(第31~46行);而getCharCount()方法获取字符中包含分隔符的数目,以得到目标字符串数组的大小(第49~57行)。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In C language, if statement is usually used to execute a specific block of code based on a single condition. However, multiple conditions can be combined to make a determination using logical operators such as &&, ||, and !. Including using logical AND (&&) to judge multiple conditions, using logical OR (||) to judge at least one condition, using logical NOT (!) to judge the negation of a single condition, as well as nesting if statements and using parentheses to clarify priority.

The "Inaction Test" of the new fantasy fairy MMORPG "Zhu Xian 2" will be launched on April 23. What kind of new fairy adventure story will happen in Zhu Xian Continent thousands of years after the original work? The Six Realm Immortal World, a full-time immortal academy, a free immortal life, and all kinds of fun in the immortal world are waiting for the immortal friends to explore in person! The "Wuwei Test" pre-download is now open. Fairy friends can go to the official website to download. You cannot log in to the game server before the server is launched. The activation code can be used after the pre-download and installation is completed. "Zhu Xian 2" "Inaction Test" opening hours: April 23 10:00 - May 6 23:59 The new fairy adventure chapter of the orthodox sequel to Zhu Xian "Zhu Xian 2" is based on the "Zhu Xian" novel as a blueprint. Based on the world view of the original work, the game background is set

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Dogecoin is a cryptocurrency created based on Internet memes, with no fixed supply cap, fast transaction times, low transaction fees, and a large meme community. Uses include small transactions, tips, and charitable donations. However, its unlimited supply, market volatility, and status as a joke coin also bring risks and concerns. What is Dogecoin? Dogecoin is a cryptocurrency created based on internet memes and jokes. Origin and History: Dogecoin was created in December 2013 by two software engineers, Billy Markus and Jackson Palmer. Inspired by the then-popular "Doge" meme, a comical photo featuring a Shiba Inu with broken English. Features and Benefits: Unlimited Supply: Unlike other cryptocurrencies such as Bitcoin

2024 is the first year of AI mobile phones. More and more mobile phones integrate multiple AI functions. Empowered by AI smart technology, our mobile phones can be used more efficiently and conveniently. Recently, the Galaxy S24 series released at the beginning of the year has once again improved its generative AI experience. Let’s take a look at the detailed function introduction below. 1. Generative AI deeply empowers Samsung Galaxy S24 series, which is empowered by Galaxy AI and brings many intelligent applications. These functions are deeply integrated with Samsung One UI6.1, allowing users to have a convenient intelligent experience at any time, significantly improving the performance of mobile phones. Efficiency and convenience of use. The instant search function pioneered by the Galaxy S24 series is one of the highlights. Users only need to press and hold

The eighth color is a weapon in Neon Abyss. Many players want to know about the ballistics of the eighth color of the weapon and how to play with the weapon strength. So let’s take a look at the detailed guide to Neon Abyss’ eighth color weapon trajectory, weapon strength, and weapon gameplay. Neon Abyss Color 8 Detailed Guide Weapon Introduction: The Wizard’s Secret Weapon! Weapon attack speed: Normal Weapon strength: Moderate Weapon gameplay: The attack method of the eighth color is three single-target attacks and then launches a ray. Ballistic display:

A fast score query tool provides students and parents with more convenience. With the development of the Internet, more and more educational institutions and schools have begun to provide online score check services. To allow you to easily keep track of your child's academic progress, this article will introduce several commonly used online score checking platforms. 1. Convenience - Parents can check their children's test scores anytime and anywhere through the online score checking platform. Parents can conveniently check their children's test scores at any time by logging in to the corresponding online score checking platform on a computer or mobile phone. As long as there is an Internet connection, whether at work or when going out, parents can keep abreast of their children's learning status and provide targeted guidance and help to their children. 2. Multiple functions - in addition to score query, it also provides information such as course schedules and exam arrangements. Many online searches are available.
