In: Computer Science
Program Code Screenshot :
CalculationTest.java
CalculationTestSuite.java
Output :
Program Code to Copy
Calculation.java
public class Calculation { //method that returns cube of the given number public int findMax(int arr[]){ int max=0; for(int i=1;i<arr.length;i++){ if(max<arr[i]){ max=arr[i]; } } return max; } //method that returns cube of the given number public static int cube(int n){ return n*n*n; } }
CalculationTest.java
import junit.framework.TestCase; public class CalculationTest extends TestCase { Calculation calculation; public CalculationTest(){ } @Override public void setUp(){ //Create a Calculation instance calculation = new Calculation(); } public void testfindMax(){ //Check for the maximums assertEquals(calculation.findMax(new int[]{}),0); assertEquals(calculation.findMax(new int[]{2,8,4,3}),8); } public void testCube(){ //Check for cube test assertEquals(Calculation.cube(0),0); assertEquals(Calculation.cube(-5),-125); assertEquals(Calculation.cube(6),216); } @Override public void tearDown(){ } }
CalculationTestSuite.java
import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Suite; import static org.junit.Assert.assertEquals; @RunWith(Suite.class) @Suite.SuiteClasses({ CalculationTest.class }) public class CalculationTestSuite { @Test public void testCube(){ assertEquals(Calculation.cube(0),0); assertEquals(Calculation.cube(-5),-125); assertEquals(Calculation.cube(6),216); } }