Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JUnit Testing 1 . Develop four JUnit tests to evaluate ArrayOperations.numZero ( ) method. Please add your JUnit tests to ArrayOperationsNumZeroTest.java. Is there a fault

JUnit Testing
1. Develop four JUnit tests to evaluate ArrayOperations.numZero() method. Please add your JUnit tests to ArrayOperationsNumZeroTest.java. Is there a fault in this method? Can your tests reveal the failure?
2. Please try designing one more test to test that the NullPointerException is thrown as expected.
3. Please try modifying/re-organizing your tests by adding test fixtures @Before and @After. It is ok to create additional test files if some tests do not share the same pre or post-conditions.
4. Calc.java provides a simple calculation method add (int, int). Please add a new method to calculate multiply (int, int).
5. Design four JUnit tests using the traditional way for the new method multiply (int, int).
6. Following the data-driven JUnit example in DataDrivenCalcTest.java, please modify your four tests into data-driven JUnit tests.
Program #1 ArrayOperations
package inclass;
public class ArrayOperations
{
// Adapted from "Introduction to Software Testing",
// by Ammann and Offutt
public static int numZero(int[] x)
{
// Effects: if x == null throw NullPointerException
// else return the number of occurrences of 0 in x
int count =0;
for (int i=1; i0; i--)
{
if (x[i]== y)
return i;
}
return -1;
}
// test: x =[2,3,5]; y =2;
// expected =0
public static int countPositive (int[] x)
{
if (x == null)
throw new NullPointerException();
int count =0;
for (int i=0; i=0)
count++;
}
return count;
}
// test: x =[-4,2,0,2]
// expected =2
public static int lastZero (int[] x)
{
if (x == null)
throw new NullPointerException();
for (int i=0; i 0)
count++;
}
return count;
}
// test: x =[-3,-2,0,1,4]
// expected =3
}
Program #2 ArrayOperationsNumZeroTest
package inclass;
import static org.junit.Assert.*;
import org.junit.Test;
public class ArrayOperationsNumZeroTest
{
@Test
public void testNumZeroEmptyArray()
{
int x[]={}; // zero-sized array
int n = ArrayOperations.numZero(x);
assertEquals("0 zeros", 0, n);
}
@Test
public void testNumZeroArrayWithNoZeros()
{
int[] x ={1,2,3};
int n = ArrayOperations.numZero(x);
assertEquals("0 zeros in an array with no zeros", 0, n);
}
}
Program #3 Calc
// Introduction to Software Testing
// Authors: Paul Ammann & Jeff Offutt
// Chapter ?; page ??
// See CalcTest.java, DataDrivenCalcTest.java for JUnit tests
public class Calc
{
static public int add (int a, int b)
{
return a + b;
}
}
Program #4 DataDrivenCalcTest
// Introduction to Software Testing
// Authors: Paul Ammann & Jeff Offutt
// Chapter ?; page ??
// JUnit for Calc.java
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import static org.junit.Assert.*;
import java.util.*;
@RunWith (Parameterized.class)
public class DataDrivenCalcTest
{
public int a, b, sum;
public DataDrivenCalcTest (int a, int b, int sum)
{
this.a = a;
this.b = b;
this.sum = sum;
}
@Parameters
public static Collection calcValues()
{
return Arrays.asList (new Object [][]{{1,1,2},{2,3,5}});
}
@Test
public void additionTest()
{
assertTrue ("Addition Test", sum == Calc.add (a,b));
}
}
Please give solution and detailed explanation for each program. Thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel 2024 In 7 Days

Authors: Alan Dinkins

1st Edition

B0CJ3X98XK, 979-8861224000

More Books

Students also viewed these Databases questions

Question

5. How can I help others in the network achieve their goals?

Answered: 1 week ago