hello could you help me create path test using a junit i got errors some and i d
ID: 3555671 • Letter: H
Question
hello could you help me create path test using a junit
i got errors some
and i dont know it is right or not format.
also whats readfilee documenting the test cases like showen table
package select;
public class selectionSort2 {
public static void main(String[] args)
{
int n = 20;
int[] numArray = unsortedArray(n); // re-initialize
printArray(numArray,n);
selectSort2(numArray, n);
printArray(numArray, n);
}
//Generate data for the selection sort array
public static int[] unsortedArray(int n) {
int[] a = new int[n];
for (int index = 0; index < n; index++) {
a[index] = (n - index);
}
return a;
}
//print the array
public static void printArray(int[] data, int n)
{
for (int index = 0; index < n; index++) {
System.out.print(data[index] + " ");
}
System.out.println();
} public static void selectSort2(int[] data, int n)
{
for (int numUnsorted = n; numUnsorted > 0; numUnsorted--) {
int maxIndex = 0;
for (int index = 1; index < numUnsorted; index++) {
if (data[maxIndex] < data[index])
maxIndex = index;
//swap the element
int temp = data[numUnsorted-1];
data[numUnsorted-1] = data[maxIndex];
data[maxIndex] = temp;
}
}
}
}
test
package select;
import static org.junit.Assert.*;
import org.junit.Test;
public class Pathtest {
@Test
public boolean testSelectSort2() {
selectionSort2 test = new selectionSort2();
int []data={20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7,
6, 5, 4, 3, 2, 1};
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20 };
int n=0;
if( n>21)
{
selectionSort2.selectSort2(data,n);
return false;
}
else if (n==0)
{
selectionSort2.selectSort2(data,n);
return false;
}
else
assertArrayEquals(expected, data);
return true; // TODO
}
}
Error:
java.lang.Exception: Method testSelectSort2 should be void
at org.junit.internal.runners.MethodValidator.validateTestMethods(MethodValidator.java:74)
at org.junit.internal.runners.MethodValidator.validateInstanceMethods(MethodValidator.java:30)
at org.junit.internal.runners.MethodValidator.validateMethodsForDefaultRunner(MethodValidator.java:41)
at org.junit.internal.runners.TestClassRunner.validate(TestClassRunner.java:33)
at org.junit.internal.runners.TestClassRunner.<init>(TestClassRunner.java:27)
at org.junit.internal.runners.TestClassRunner.<init>(TestClassRunner.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:41)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
at java.lang.reflect.Constructor.newInstance(Constructor.java:515)
at org.junit.internal.requests.ClassRequest.buildRunner(ClassRequest.java:33)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:28)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:28)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:445)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Explanation / Answer
JUST MAKE TEST CASE FILE AS THIS
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
public class Pathtest {
@Test
public void testSelectSort2() {
selectionSort2 test = new selectionSort2();
int []data={20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7,
6, 5, 4, 3, 2, 1};
int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20 };
int n=20;
if( n>21)
{
selectionSort2.selectSort2(data,n);
System.out.println("Test case failed");
return;
}
else if (n==0)
{
selectionSort2.selectSort2(data,n);
System.out.println("Test case failed");
return;
}
else {
selectionSort2.selectSort2(data,n);
assertArrayEquals(expected, data);
}
System.out.println("Test case passed");
}
}
DOCUMENTATION OF TEST CASES :
PURPOSE : To Test Selection Sort Working, and also to check if input numbers are between 1 and 20
REQUIREMENT : Need only a JAVA IDE with JUNIT 4 Working
SETUP : You need to create package 'select' and in that package put the java source file with classname 'selectionSort2' and then within test section create a class 'PathTest'
TEST DATA : An already sorted array of size 20 and an array to be sorted of the same size.
PROCEDURE : Start running the test, It will call SelectionSort2 class and from there it will call selectionSort method and then will sort the given array.
After the sorting, the two arrays are compared, and if they are equal means sorting is tested. So it will print success or else if it fails it will message failure.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.