Jakub Szarubka :
I need to implement my own List.size() method. I create such a method :
public class MyArrayList<Long> implements List<Long> {
List<Long> list;
@Override
public int size() {
if(list == null) {
throw new NullPointerException("List is null");
}
int size = 0;
for(Long element : list) {
size++;
}
return size;
} I write an abstract test class to do the test for the existing method of size and mine.
public abstract class AbstractArrayListTest {
protected abstract List<Long> getEmptyList();
protected abstract List<Long> getSizeOfTheList();
@Test
public void shouldReturnFalseIfListContainElement() {
assertTrue(getEmptyList().isEmpty());
}
@Test
public void shouldReturnSizeOfList() {
int size = getSizeOfTheList().size();
assertEquals(size, getEmptyList().size());
}
}
Every time I try to test it a get null E. Any thought how I can write it ? Worth to mention the original implementation of List is passing this test.
Andronicus :
You're not initializing the list you're wrapping over. When you're trying to iterate over it, you're getting a NPE. Please, try the following class definition:
public class MyArrayList<Long> implements List<Long> {
List<Long> list = new ArrayList<>();
// rest of the code
}