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)
? Use classes and methods (write a method for greatest common divisor), and name them according to the specification.
? in the main mode, get the two integers entered by the user, call the previously written method, and output their greatest common divisor.
? use FindBugs to find out if a bug exists in the program.
Packagecn.lq.com;ImportJava.io.*;ImportJava.util.*;classDemo { Public Static voidMain (String args[])throwsException {Scanner cin=NewScanner (system.in); System.out.println ("Please enter the first number:"); intA =Cin.nextint (); System.out.println ("\ n Please enter a second number:"); intb =Cin.nextint (); intc =gcd (A, b); System.out.println ("\ n Greatest Common divisor:" +c); } Public Static intgcdintAintb) { while(true) { if((a = a% b) = = 0) returnb; if((b = b% a) = = 0) returnA; } } }
No syntax errors
2, the application of logic coverage
? write Java code (implemented using classes and methods) according to the program flowchart given
? Write a test case for a statement overlay, a branch overlay, and the path it covers, test with JUnit
Packagecn.lq.com;ImportJava.util.Scanner; Public classDemo {/** * @paramargs*/ Public Static voidTfloatXfloaty) {System.out.print ("The path passed is a");if(x<4| | Y>0) {System.out.print ("B");if(y>1) {System.out.println (C); y=y+1; System.out.println ("X=" +x); System.out.println ("Y=" +y);}Else{System.out.println ("D"); System.out.println ("End judgment"); System.out.println ("X=" +x); System.out.println ("Y=" +y);}}Else{System.out.print (E);if(x>=5) {System.out.println ("F"); x=x-y; System.out.println ("X=" +x); System.out.println ("Y=" +y);}Else{System.out.println ("G"); x=x+y; System.out.println ("X=" +x); System.out.println ("Y=" +y); }}}
Test class
package cn.lq.com; import static org.junit.assert.*; import Org.junit.Test; public class Demotest {@Test public void Test () { float x = 2; float y = 1 "test two numbers for x=" +x+ "y=" +y+ ""
Statement overrides
Test Cases |
Overwrite use cases |
Results |
< Span style= "font-family: ' Courier New ';" >x=4 y=0 |
aeg |
x=4 y=0 |
< Span style= "font-family: ' Courier New ';" >x=5 y=0 |
aef |
x=5 y=0 |
X=2 y=2 |
Adc |
X=2 Y=3 |
Branch Overrides
< Span style= "font-family: ' Courier New ';" > test case |
overwrite use case |
result |
< Span style= "font-family: ' Courier New ';" >x=4 y=0 |
aeg |
x=4 y=0 |
< Span style= "font-family: ' Courier New ';" >x=5 y=0 |
aef |
x=5 y=0 |
< Span style= "font-family: ' Courier New ';" >x=2 y=2 |
ADC |
x=2 y=3 |
x=2 Y=1 |
Abd |
x=2 Y=1 |
We test the statements covered by the
Experiment Three white Box test