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?
You're code may not be running as you expected, but it is running exactly as a more experienced Java dev would expect. Let me explain...
You've discovered the behavior of the =
operator (or more precisely in this case, !=
) that often trips up less experienced Java engineers. The =
operator doesn't know how to work with arrays so it falls back to comparing references. In your case it is comparing input
and expected
to see if they reference the exact same object. In your code both input
and expected
are declared as new arrays and therefor are different, individual objects; they do not reference the same object.
As for assertArrayEquals
it likely doesn't use the =
operator at all. While I haven't looked at the source code for that method I'd venture to guess that it first checks reference equality (are they both referencing the same object, then checks to see if they are both arrays and then checks to see whether each element of expected
is also in input
.
Arrays can add to the equality confusion because there are many definitions of equality. Equality could be defined as...
One suggestion I have that might help you better understand this issue (as well as many more issues you are likely to face in the future) is to look at the source code of the method that's not working as you expect it to work, assertArrayEquals
in this case. IntelliJ allows you to navigate to the source code, or if the source code is not available, look at the decompiled byte code. On a Mac just Command-click on the method. In Windows it might be Control-click??? (sorry, IntelliJ has so many different shortcut sets I can't be more specific.)
Further info on this topic... What is the difference between == vs equals() in Java? https://javabeginnerstutorial.com/core-java-tutorial/java-equals-method-vs-operator/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.