The function to be tested is described below:
(a) Verify that the mailbox format is correct
public static Boolean Checkmail (String mail);
Description: The valid e-mail address verified by this function:
Must contain one and only one symbol "@"
The first character must not be "@" or "."
do not allow "@." or [email protected]
The end must not be the character "@" or "."
Allow "+" to appear in characters before "@"
"+" is not allowed on the front, or "+@"
(b) Verify that the QQ number format is correct
public static string Checkqq (String qq);
Description: Legal QQ Number:
5 to a level.
each digit must be a number
0 cannot begin
c) Convert a string into a number
public static int stringcasttoint (String str);
Description: Example string 0 is converted to a number 0, the string 1234 is converted to a digital 1234,
string -1234 is converted to the number -1234, and the string ABCD cannot be converted to a number.
(d) find the first occurrence of a character in a string
public static char Tofirstchar (String str);
Description: Example "ab3443ac" The first occurrence of the character is 'b'.
based on well-designed test cases, we use the JUnit Framework for test script development.
The main steps are as follows:
① in MyEclipse New Java project, introduced in the project JUNIT4 .
② Import the tool class that needs to be tested, create a new test case, and then write a test script.
③ to JUnit Test Run the project
④ Analysis Test Results
The test class code is as follows:
in the String function (a) test script writing, we should be the input string is not converted to a number, in the test we expect the result is that the function throws an exception, we can use the try catch to catch the exception, note must be called Fail () this method. After consulting the information assert.fail () The role is: Add in the expected impossible to reach, once arrived, indicating that the test failed, the results are different from the expectations.
The test results are as follows: after writing the test script, run the project in JUnit and test the results such as:
by means of the total test failed 3 testtochar () , TESTCAT1 () testcast () The other three function tests passed.
①: First of all, we testcast () analysis, according to the test script written, the test does not pass because the function input fd2343,21314.34 The time function does not throw an exception. We analyze the function source code, originally the function did not judge the input, regardless of the input string, is to convert its ASCII code. Tested testcast (fd2343), the output is 5922343, so the function in the string into the correct number is not a problem, but the input is not judged. function Improvement: The input is judged, and an exception is thrown when the input string cannot be converted to a number.
②: Then we have Testtochar () analysis, according to the test script written, the actual output of the function different from the expected output. We can print out the actual output of the function, and after analyzing all the output results, the output of the function is the last character of the string that appears only once.
System.out.println (Baseutil.tofirstchar ("012345"));//5
System.out.println (Baseutil.tofirstchar ("Ab3443ac"));//c
System.out.println (Baseutil.tofirstchar (" -23-23++y"));//y
System.out.println (Baseutil.tofirstchar ("Yyczzc"));//y
The parsing function code shows that when a character is found in a traversal lookup, the function does not return .
function Improvements: Find the word specifier break when traversing a lookup .
Third time job