Experimental purpose
(1) Learn white box test method
(2) Mastering the logic covering methods of statement coverage, conditional coverage, branch coverage, etc.
(3) Mastering the use of Java Code Analysis tools
Experimental content
1. computes the greatest common divisor of an integer x and an integer y. (not allowed in the way the classroom is used)
L Use classes and methods (define a class, define a greatest common divisor method in the Class), and name it according to the specification.
L GET the two integers entered by the user in the main method, call the previously written method, and output their greatest common divisor.
L use FindBugs to find out if there are any bugs in the program.
Code:
import Java.io.BufferedReader;
import java.io.IOException;
import Java.io.InputStreamReader;
Public class CE {
Public Static void Main (string[] args) {
int a = 0;
System. out. Print ("Please enter a number:");
BufferedReader strin=New BufferedReader (new InputStreamReader (System. in));
Try {
A=integer. parseint (Strin.readline ());
} catch (NumberFormatException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
int b = 0;
System. out. Print ("Please enter a number:");
BufferedReader strin2=New BufferedReader (new InputStreamReader (System. in));
Try {
B=integer. parseint (Strin2.readline ());
} catch (NumberFormatException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
int flag = 0;
for (int i = 1; i <= A; i++) {
if (a%i==0) {
if (b%i==0) {
Flag=i;
}
}
}
System. out. println ("Greatest common divisor for" +flag);
}
}
2. Application of Logic overlay
L write out the test cases covered by the statement, the branch coverage, and the path it covers, according to the program flowchart given.
L Add-on: According to the program flowchart, write out the code (define a class and method to implement), generate unit tests with JUnit, and test using the test cases previously designed.
Statement overrides:
AEG X=4,y=0
AEF x=5,y=0
Abd x=3,y=0
ABC x=3,y=2
Branch Coverage:
X=4,y=0
X=5,y=0
X=3,y=3
x=3,y=2
Code:
import Java.util.Scanner;
Public class Shithree {
Public Static void Main (string[] args) {
TODO auto-generated Method stub
TODO auto-generated Method stub
Shithree a = new shithree ();
Scanner in =new Scanner (System. in);
System. out. println ("Please enter A:");
double x=in.nextdouble ();//This is the input x
System. out. println ("Please enter B:");
double y=in.nextdouble ();//This is the input y
A.shu (x, y);
}
Public void shu (double n,double m) {
if (m<4| | n>0) {
if (n>1) {
n=n+1;
System. out. println ("y=" +n);
System. out. println ("x=" +m);
}Else{
System. out. println ("y=" +n);
System. out. println ("x=" +m);
}
}Else if(m>=5) {
M=m-n;
System. out. println ("y=" +n);
System. out. println ("x=" +m);
}Else{
M=m+n;
System. out. println ("y=" +n);
System. out. println ("x=" +m);
}
return;
}
}
Software Test-experimental experiment three white box test