P02 { public static <T> T secondLast(List<T> list) { if (list.size() < 2) { throw new NoSuchElementException("Can't find secondLast element from a list with less than 2 elements"); } return list.get(list.size() - 2); }  static T secondLast(List<T> list); static T secondLastRecursion(LinkedList<T> list);  }
@Test public void shouldFindSecondLastElementFromAList() throws Exception { List<Integer> numbers = asList(1, 2, 11, 4, 5, 8, 10, 6); assertThat(P02.secondLast(numbers), is(equalTo(10))); }
@Test(expected = NoSuchElementException.class) public void shouldThrowExceptionWhenListEmpty() throws Exception { P02.secondLast(Collections.emptyList()); }
@Test(expected = NoSuchElementException.class) public void shouldThrowExceptionWhenListHasSingleElement() throws Exception { P02.secondLast(Arrays.asList(1)); }