Answered step by step
Verified Expert Solution
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
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?
Please try designing one more test to test that the NullPointerException is thrown as expected.
Please try modifyingreorganizing 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 postconditions.
Calc.java provides a simple calculation method add int int Please add a new method to calculate multiply int int
Design four JUnit tests using the traditional way for the new method multiply int int
Following the datadriven JUnit example in DataDrivenCalcTest.java, please modify your four tests into datadriven JUnit tests.
Program # ArrayOperations
package inclass;
public class ArrayOperations
Adapted from "Introduction to Software Testing",
by Ammann and Offutt
public static int numZeroint x
Effects: if x null throw NullPointerException
else return the number of occurrences of in x
int count ;
for int i; i; i
if xi y
return i;
return ;
test: x ; y ;
expected
public static int countPositive int x
if x null
throw new NullPointerException;
int count ;
for int i; i
count;
return count;
test: x
expected
public static int lastZero int x
if x null
throw new NullPointerException;
for int i; i
count;
return count;
test: x
expected
Program # ArrayOperationsNumZeroTest
package inclass;
import static org.junit.Assert.;
import org.junit.Test;
public class ArrayOperationsNumZeroTest
@Test
public void testNumZeroEmptyArray
int x; zerosized array
int n ArrayOperations.numZerox;
assertEquals zeros", n;
@Test
public void testNumZeroArrayWithNoZeros
int x ;
int n ArrayOperations.numZerox;
assertEquals zeros in an array with no zeros", n;
Program # 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 # 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 Parameterizedclass
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 ;
@Test
public void additionTest
assertTrue Addition Test", sum Calc.add ab;
Please give solution and detailed explanation for each program. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started