-5

I have a list of numbers and I'm trying to write a jUnit test that checks to make sure the list is being populated and returned correctly. I'm running into a problem where I can't figure out how to write the correct jUnit test. Here's what I come up with:

ArrayListPractice.java

import java.util.ArrayList;

public class ArrayListPractice {
    ArrayList<String> myList = new ArrayList<String>();

    public ArrayList<String> addToList(String item) {
        myList.add(item);
        return myList;
    }

    public ArrayList<String> printList(ArrayList<String> myList) {
        for (String currentString : myList) {
            System.out.println(currentString);
        }
        return myList;
    }
}

ArrayListPracticeTest.java

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ArrayListPracticeTest {
    ArrayList<String> myList = new ArrayList<>();
    ArrayListPractice alp;

    @Before
    public void setUp() throws Exception {
        alp = new ArrayListPractice();
        alp.addToList("ONE");
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void testPrintList() {
        String expected = "ONE";
        ArrayList<String> actual = alp.printList(myList);
        assertEquals("not the correct list", expected, actual);
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
user3767997
  • 83
  • 3
  • 10
  • What do you think `void` means? What do you think `assertEquals` does? – Sotirios Delimanolis Sep 13 '15 at 00:22
  • @SotiriosDelimanolis void indicates that there's no return value and assertEquals is used to check whether the actual and expected values are equal – user3767997 Sep 13 '15 at 00:24
  • Ok, now put those answers together while reading the error message given by your compiler. – Sotirios Delimanolis Sep 13 '15 at 00:25
  • @SotiriosDelimanolis I'm pretty new to Java, so my apologies if I don't understand as quick as I should. I've changed my code above to reflect the changes I made and I'm now getting a NullPointerException error. – user3767997 Sep 13 '15 at 00:36
  • http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – Sotirios Delimanolis Sep 13 '15 at 00:37
  • @SotiriosDelimanolis I've updated my code above. At this point I'm still getting an error where it says expected: but was:<[]> and I get that this means that the ArrayList is not getting populated correctly. – user3767997 Sep 13 '15 at 00:46
  • `assertEquals("not the correct list", expected, actual);` .. I translate it for you: assert that _String_ `expected` is equal to the _List_ `actual`. You may now see, why wait can't be equal. And `alp.printList(myList);` is also wrong ... besides: why does the `printList` needs a parameter anyway? – Tom Sep 13 '15 at 01:11
  • @Ikmac, no probs. I'll delete mine as well. – MetaFight Sep 13 '15 at 13:08

1 Answers1

0

While I have deep reservations over the quality of this code, the problem is that you're not comparing apples to apples here. You expect a String, but you're getting back an ArrayList. Thus, the test fails with a false positive; you didn't set the test up correctly

The fix is to ensure that what you expect is what you actually get back.

final ArrayList<String> expected = Arrays.asList("ONE");

Now, to fix the actual code, I encourage you to get rid of the parameter. I leave that as an exercise for the reader.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • I would prefer `final List expected = Arrays.asList("ONE");` as it doesn't use a technique that newbies may not understand :P. – Tom Sep 13 '15 at 01:28
  • @Tom: That's a good catch, and I've corrected it, but I'd feel pretty bad for a newbie that decided to blanket-copy any code they found on the Internet and give it a try in their application. – Makoto Sep 13 '15 at 01:29
  • You might also correct the variable type :P. And yes, this would be bad, but I guess we know that this will happen. Maybe not with this OP, but with other readers :(. – Tom Sep 13 '15 at 01:31
  • @Makoto I'm going through my code trying to apply the changes above and you mentioned that the quality of my code isn't the best. What would you suggest for me to make it better? – user3767997 Sep 13 '15 at 01:32
  • @user3767997 Two things: why does `printList` expect an argument? And: [What does it mean to "program to an interface"?](http://stackoverflow.com/q/383947) – Tom Sep 13 '15 at 01:35
  • @Tom there was no reason behind printList having an argument. I deleted that lol. Reading through that makes me realize that my code could have been so much cleaner! – user3767997 Sep 13 '15 at 01:46