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);
}
}