Experiment Three white Box test

Source: Internet
Author: User
Tags greatest common divisor

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.