In: Computer Science
* Word file clearly describe the test suite (series of test cases) you design for each of the methods in TestWithJUnit.java (one test suite per method). Each test suite should contain at least 5 test cases. Each test case has to be justified: Why did you pick this test case and not another one? Imagine you are limited by time and money about the number of test cases you can pick and run. Why would you make run the test cases you propose? You have to be convincing. In particular, you have to address: WHAT each test case aims to test, and HOW you expect the method to run on this test case (what output do you expect?).
*In a new java file, that you will call TestWithJUnitTester.java, write a JUnit test for each of the test cases you have described in your word file.
* Run your test cases and report the results in your word document. In particular, you have to report whether the method behaves as expected or not on each test case, and propose an explanation in case the method does not behave as expected.
Note: your test cases cannot include the examples given within the code.
Advice: when designing test cases, think:
1/ regular functionality test: does the code perform as expected under normal/expected circumstances?
2/ edge case: does the code still perform when under stress of its expected conditions?
You need to have at least one of the first type (maybe two depending on how complex the code is), and 3 or 4 of the second type.
public class TestWithJUnit {
/* Method withoutTen:
* Return a version of the given array where all
* the 10's have been removed.
* The remaining elements should shift left
* towards the start of the array as needed,
* and the empty spaces a the end of the array
* should be 0.
* So {1, 10, 10, 2} yields {1, 2, 0, 0}.
* {1, 10, 10, 2, 10, 3, 10} yields {1, 2, 3, 0, 0, 0,
0}.
*/
public int[] withoutTen(int[] A) {
int[] result = new
int[A.length];
int index = 0;
for (int i = 0; i < A.length;
i++) {
if (A[i] != 10)
{
result[index] = A[i];
index++;
}
}
for (int i = index; i <
result.length; i++)
result[i] =
0;
return result;
}
/* Method bigArray:
* Given an integer n, bigArray creates and returns a
1D array
* that contains {1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2,
... n}
* For instance, bigArray(4) = {1, 1, 2, 1, 2, 3, 1, 2,
3, 4}
* bigArray(2) = {1, 1, 2}
*/
public int[] bigArray(int n) {
int[] result = new
int[n*(n+1)/2];
int index = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1;
j <= i; j++) {
result[index] = j;
index++;
}
}
return result;
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// TestWithJUnitTester.java
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class TestWithJUnitTester {
// TestWithJUnit object
TestWithJUnit test;
@Before
public void setup() {
// initializing object of TestWithJUnit class
test = new TestWithJUnit();
}
// suite testing withoutTen() method
@Test
public void testWithoutTen() {
// test case 1 - normal case - an array containing some elements as 10,
// and some elements as other than 10.
int case1[] = { 1, 2, 10, 3, 4, 10, 10 }; // input array
int expected1[] = { 1, 2, 3, 4, 0, 0, 0 }; // expected output array
assertArrayEquals(test.withoutTen(case1), expected1);
// test case 2 - edge case 1 - an array containing 10s only
int case2[] = { 10, 10, 10, 10, 10 };
int expected2[] = { 0, 0, 0, 0, 0 };
assertArrayEquals(test.withoutTen(case2), expected2);
// test case 3 - edge case 2 - an array containing no 10s
int case3[] = { 1, 2, 3, 4, 5 };
int expected3[] = { 1, 2, 3, 4, 5 };
assertArrayEquals(test.withoutTen(case3), expected3);
// test case 4 - edge case 3 - empty array
int case4[] = {};
int expected4[] = {};
assertArrayEquals(test.withoutTen(case4), expected4);
// test case 5 - edge case 4 - an array containing only one 10
int case5[] = { 10 };
int expected5[] = { 0 };
assertArrayEquals(test.withoutTen(case5), expected5);
}
// suite testing bigArray() method
@Test
public void testBigArray() {
// test case 1 - normal case - passing a positive integer value
int case1 = 3;
int expected1[] = { 1, 1, 2, 1, 2, 3 };
assertArrayEquals(test.bigArray(case1), expected1);
// test case 2 - edge case 1 - passing 0 as value for n
int case2 = 0;
int expected2[] = {};
assertArrayEquals(test.bigArray(case2), expected2);
// test case 3 - edge case 2 - passing 1 as value for n
int case3 = 1;
int expected3[] = { 1 };
assertArrayEquals(test.bigArray(case3), expected3);
// test case 4 - edge case 3 - passing negative value
int case4 = -5;
int expected4[] = {};
// this test will fail since bigArray() method is not accounted for
// negative input values. I'm just including this for fair and square
// testing, if you need this. just uncomment below line
// assertArrayEquals(test.bigArray(case4), expected4);
}
}
/*OUTPUT of testing*/