简体   繁体   中英

Simple JUnit tests keep failing and I am not sure why

We were given the assignment to write several methods to help add functionality to a TodoList. One of the methods wanted was one that goes through the TodoList and returns the ones which are still incomplete.

This is what I have written (with other methods omitted):

public class TodoList {
    
    private ArrayList<TodoItem> todoItems;

    public TodoList() {
       todoItems = new ArrayList<TodoItem>();
    }

    public List<TodoItem> filterIncompletes () {
      List<TodoItem> incs = todoItems; *****
      for (int i = 0; i <todoItems.size(); i++) {
        if (todoItems.get(i).isCompleted() == false) {
             incs.add(todoItems.get(i));
        }
      }
      return incs;
    }

I have put asterisks on the line where I believe to be going wrong, although I am not positive. I understand that the method wants a List<TodoItem> returned, I'm just not sure if I have that correctly.

Change

List<TodoItem> incs = todoItems;

to

List<TodoItem> incs = new ArrayList<TodoItem>();

Seems like you want to create a new List to add to. By assigning to the same one as the internal list you are appending to it.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM