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 integer x and integer y. (not allowed in the way the classroom is used)
L Use classes and methods (write a method for greatest common divisor), and name them according to the specification.
L GET the two integers entered by the user in main mode, call the methods written before, and output their greatest common divisor.
L use FindBugs to find out if there are any bugs in the program.
import Java.util.Scanner;
Public class common{
Beg Greatest common divisor
Public Static int commondivisor (int n,int m) {
The Division is divided by the large divide by the small. If n<m, for the first time quite n and m value Exchange
while (n%m!=0) {
int temp=n%m;
N=m;
M=temp;
}
return m;
}
Public Static void Main (string[] args) {
Scanner sc=New Scanner (System. in);
System. out. println (" Please enter the first number:");
int n=sc.nextint ();
System. out. println (" Please enter a second number:");
int m=sc.nextint ();
System. out. println (" greatest common divisor for:"+commondivisor(n,m));
}
}
After the mistake
2, the application of logic coverage
• Write Java code (implemented using classes and methods) in accordance with the program flowchart given
Write a test case that is overwritten by a statement, a branch overlay, and the path it covers, and test with JUnit to write test cases
import Java.util.Scanner;
Public class Test1 {
/**
* @param args
*/
Public void Test () {
Scanner sc=New Scanner (System. in);
System. out. println ("Please enter the value of x:");
int x=sc.nextint ();
System. out. println ("Please enter the value of y:");
int y=sc.nextint ();
if (X<4 | | y>0) {
if (y>1) {
Y=y+x;
}
Else {}
}
Else {
if (x>=5) {
X=x-y;
}
Else {
X=x+y;
}
}
System. out. println ("x=" +x);
System. out. println ("y=" +y);
}
}
Test class
Import static org.junit.assert.*;
Import Org.junit.Test;
public class Test1test {
Test1 test1 =new Test1 ();
@Test
public void Test () {
Test1.test ();
}
}
Statement overrides:
x=4,y=0 Path:AEG
x=5,y=0 Path:AEF
x=4,y=2 path:ABC
Branch Coverage:
x=4,y=0 Path:AEG
x=5,y=0 Path:AEF
x=4,y=2 path:ABC
X=3,y=1 Path : Abd
Experiment Three white Box test