Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

White Box Testing Given the following method in Java, which finds the maximum va

ID: 3840552 • Letter: W

Question

White Box Testing

Given the following method in Java, which finds the maximum value in an array of integers.

public static int maximumValue(int[] a) {

   if (a != null && a.length > 0) {

     int max = a[0];

     for (int i = 1; i < a.length; i++) {

       if (a[i] > max) {

         max = a[i];

       }

     }

     return max;

   }

   return Integer.MIN_VALUE;

}

Design a test suite with the fewest number of test cases that satisfies the statement test criterion.

Design a test suite with the fewest number of test cases that satisfies the branch-condition test criterion.

Explanation / Answer

Create seperate Test Cases for every possible cases to Test the given method.

Since the class name is not provided, I assume the class where the maximumValue() method is
present as FindMax.java.

The possible scenarios are:
int[] all_equals = {0,0,0,0,0};         // TestCase 1 returns: 0
int[] all_positive = {1,2,3,4,5};    // TestCase 2 returns: 5
int[] all_negative = {-1,-2,-3,-4,-5}; // TestCase 3 returns: -1
int[] out_of_size = {1,2,3,4,5,6};     // TestCase 4 returns: Integer.Min_Value
int[] mixed = {-10,10,3,5,-6};         // TestCase 5 returns: 10
int[] empty = {};                     // TestCase 6 returns: Integer.Min_Value


I will show for 2 scenarios, you can extend to all the six scenarios.
-----------------------------------------------------------------------------------
For Scenario 1: Create file called TestJunit1.java with below code.

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class TestJunit1 {

int[] all_equals = {0,0,0,0,0};  
int retVal = 0;
FindMax getMax = new FindMax();

@Test
public void testEquals() {  
System.out.println("Inside testEquals()");
assertEquals(retVal, getMax.maximumValue(all_equals));   
}
}

-----------------------------------------------------------------------------------
For Scenario 2: Create file called TestJunit2.java with below code.

import org.junit.Test;
import org.junit.Ignore;
import static org.junit.Assert.assertEquals;

public class TestJunit2 {

int[] all_positive = {1,2,3,4,5};
int retVal = 5;
FindMax getMax = new FindMax();

@Test
public void testPositive() {  
System.out.println("Inside testPositive()");
assertEquals(retVal, getMax.maximumValue(all_positive));   
}
}
-----------------------------------------------------------------------------------
Similarly write seperate files for other scenarios as mentioned above.

Now to have the test suite, please create below files with the code as below.
-----------------------------------------------------------------------------------
Create a file TestSuite.java

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)

@Suite.SuiteClasses({
TestJunit1.class,
TestJunit2.class
// Include other classes as you add them.
})

public class JunitTestSuite {   
}

-----------------------------------------------------------------------------------
Create TestRunner.java file

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitTestSuite.class);

for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
      
System.out.println(result.wasSuccessful());
}
}
-----------------------------------------------------------------------------------

Compile all the classes as below:
$javac FindMax.java TestJunit1.java TestJunit2.java JunitTestSuite.java TestRunner.java

Now run the Test Runner, which will run the test case defined in the provided Test Case class.
$java TestRunner

OUTPUT:
Inside testPrintMessage()
true
Inside testPositive()
true