0

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

enter image description here

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?

RedBean
  • 1
  • 2
  • Welcome to stack overflow! Please go through https://stackoverflow.com/editing-help to format your question better next time you post/edit. Can you share your code for `Sort` class and the `testSort` test which is not failing? – Niks Dec 28 '18 at 04:05
  • Hi, I have edited my question, and thanks for your replying. pls give me some suggestions if you familiar with java, Junit library or IntelliJ Platform. – RedBean Dec 28 '18 at 05:05
  • Your question about running in IntelliJ vs compiling and running from the command line should be its own StackOverflow question. The short answer is yes, there is a big difference. IntelliJ does a lot of classpath magic for you whereas the command line is the wild west. Tt also depends on your definition of "command line." Are you using Maven, gradle, ant or straight up JDK tools? – HairOfTheDog Dec 28 '18 at 05:07
  • It is a straight JDK tool. – RedBean Dec 28 '18 at 05:13
  • I only want to get the green part as the video presented. I am using the Junit for testing my own code. However, When I run it, it did not give me any result as the second showed(the green part) – RedBean Dec 28 '18 at 05:15
  • You should note that there is no video in your question. All links point to a static image – HairOfTheDog Dec 28 '18 at 17:59
  • When you say "green part" are you talking about the green check mark next to each test name in the bottom left corner of the IntelliJ window? If so those green check marks are put there by IntelliJ's JUnit runner, not JUnit itself. Also, I think you might be misunderstanding what JUnit's assertion methods do. When an assertion method fails it throws an exception. When an assertion method succeeds it does *nothing*; it just returns silently. – HairOfTheDog Dec 28 '18 at 18:06
  • Thanks, I UNDERSTOOD Junit's assertions now. – RedBean Dec 29 '18 at 01:07

1 Answers1

0

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...

  • both arrays having the same number of elements in the same order
  • both arrays having the same number of elements, but different order
  • one array having 5 elements while the other having 10 elements where all 5 elements of the first array are also in the second array
  • etc.

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/

HairOfTheDog
  • 2,489
  • 2
  • 29
  • 35
  • Thanks a lot for replying back. However, my problem is that how to show the green part in the second picture, which from my point of view is a IntelliJ and Junit problem. While, the problem you mentioned about "=" and equals() is another great problem I will also do some research. Thanks – RedBean Dec 28 '18 at 05:10
  • set a breakpoint on line 8 and look at the value of `input` both before and after stepping over the call to `Sort.sort(input)` – HairOfTheDog Dec 28 '18 at 05:27
  • Jus tried adding breakpoint, the Input and the function Sort.sort() GOES WELL , but the problem happens when it goes to org.junit.Asserts. ... showed something like it doesn't have the source of Junit, More details about this problem pls see the question again I put the pics on – RedBean Dec 28 '18 at 06:33