Junit did not show any results using IntelliJ for UCB cs61B (org.junit.Assert.assertArrayEquals() )

Multi tool use
Multi tool use












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?










share|improve this question
























  • Welcome to stack overflow! Please go through 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?
    – Nikhil
    Dec 28 '18 at 4: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 5: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 5:07










  • It is a straight JDK tool.
    – RedBean
    Dec 28 '18 at 5: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 5:15
















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?










share|improve this question
























  • Welcome to stack overflow! Please go through 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?
    – Nikhil
    Dec 28 '18 at 4: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 5: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 5:07










  • It is a straight JDK tool.
    – RedBean
    Dec 28 '18 at 5: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 5:15














0












0








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?










share|improve this question















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?







java intellij-idea junit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 6:37

























asked Dec 28 '18 at 3:08









RedBean

12




12












  • Welcome to stack overflow! Please go through 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?
    – Nikhil
    Dec 28 '18 at 4: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 5: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 5:07










  • It is a straight JDK tool.
    – RedBean
    Dec 28 '18 at 5: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 5:15


















  • Welcome to stack overflow! Please go through 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?
    – Nikhil
    Dec 28 '18 at 4: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 5: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 5:07










  • It is a straight JDK tool.
    – RedBean
    Dec 28 '18 at 5: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 5:15
















Welcome to stack overflow! Please go through 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?
– Nikhil
Dec 28 '18 at 4:05




Welcome to stack overflow! Please go through 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?
– Nikhil
Dec 28 '18 at 4: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 5: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 5: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 5:07




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 5:07












It is a straight JDK tool.
– RedBean
Dec 28 '18 at 5:13




It is a straight JDK tool.
– RedBean
Dec 28 '18 at 5: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 5:15




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 5:15












1 Answer
1






active

oldest

votes


















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/






share|improve this answer























  • 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 5: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 5: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 6:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953249%2fjunit-did-not-show-any-results-using-intellij-for-ucb-cs61b-org-junit-assert-as%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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/






share|improve this answer























  • 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 5: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 5: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 6:33
















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/






share|improve this answer























  • 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 5: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 5: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 6:33














0












0








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/






share|improve this answer














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/







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 4:54

























answered Dec 28 '18 at 4:39









HairOfTheDog

1,03511525




1,03511525












  • 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 5: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 5: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 6:33


















  • 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 5: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 5: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 6:33
















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 5:10






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 5: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 5:27






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 5: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 6:33




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 6:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953249%2fjunit-did-not-show-any-results-using-intellij-for-ucb-cs61b-org-junit-assert-as%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







R,Yd7Vvvk g7Iz9d1DAO4DWQ8 zh,L sMyFZ6TpFLLh DAhQoogV Hdm,tgedc8Z2AgiWXivXJnSrkumn6IGrK,L
GonVqv1 H0xwj DKzvv2q2eie8N3UI0APj5W scg FXouQgb m8VjDsAN0O1VJl,Eb,P5hpj3 2XIYXVKF5eRe7VGEq9g6

Popular posts from this blog

Monofisismo

compose and upload a new article using a custom form

“attempting to read past stream EOM” using Sybase.AdoNet4.AseClient