I am studying CS61B - UCB on my own, and I am a beginner in using IntelliJ and Junit4.12. I found there is no result for my org.junit.Assert.assertArrayEquals()
while in the video there is something shows like this
in the Run Window.
Here is the code for TestSort.java
import static org.junit.Assert.*;
import org.junit.Test;
/** Tests the the Sort class. */
public class TestSort {
/** Test the Sort.sort method. */
@Test
public void testSort() {
String[] input = {"i", "have", "an", "egg"};
String[] expected = {"an", "egg", "have", "i"};
Sort.sort(input);
if (input != expected)
{
System.out.println("something wrong!");
}
org.junit.Assert.assertArrayEquals(expected, input);
}
@Test
public void testFindSmallest() {
String[] input = {"i", "have", "an", "egg"};
int expected = 2;
int actual = Sort.findSmallest(input, 0);
assertEquals(expected, actual);
String[] input2 = {"there", "are", "many", "pigs"};
int expected2 = 2;
int actual2 = Sort.findSmallest(input2, 2);
assertEquals(expected2, actual2);
}
@Test
public void testSwap() {
String[] input = {"i", "have", "an", "egg"};
int a = 0;
int b = 2;
String[] expected = {"an", "have", "i", "egg"};
Sort.swap(input, a, b);
assertArrayEquals(expected, input);
}
}
Here is the code for Sort.java
public class Sort {
public static void sort(String[] x) {
sort(x, 0);
}
private static void sort(String[] x, int start) {
if (start == x.length) {
return;
}
int smallestIndex = findSmallest(x, start);
swap(x, start, smallestIndex);
sort(x, start + 1);
}
public static void swap(String[] x, int a, int b) {
String temp = x[a];
x[a] = x[b];
x[b] = temp;
}
public static int findSmallest(String[] x, int start) {
int smallestIndex = start;
for (int i = start; i < x.length; i += 1) {
int cmp = x[i].compareTo(x[smallestIndex]);
if (cmp < 0) {
smallestIndex = i;
}
}
return smallestIndex;
}
}
I think the function for Junit is to get the green part which shows how my codes work and get the result of whether two of my Strings are equal or not.
Another question about the IntelliJ is whether there is any difference between I RUN it and using the terminal to compile and operate it? Because when I use terminal, it will show something like this
I have googled a lot about this, it always said like I did not applied the Junit.jar into classpath. I have checked I have added the library.enter image description here
fyi, the you can get the library here enter link description here
I debugged the testSort function and it goes well for the input part and the sort functions part. while it gives me the hint that enter image description here, I chosed Download, it showed sources not found enter image description here, and when I chose sources from exist files enter image description here, it keeps attaching....How can I solve this problem?