### Question:
ResultExtractor { public Object extractObjectFromList(List<Object> list, Class<?> targetType) { Object value = null; if (targetType != null && targetType.isAssignableFrom(list.getClass())) { value = list; } else if (targetType != null && objectFactory.isCollection(targetType)) { value = objectFactory.create(targetType); MetaObject metaObject = configuration.newMetaObject(value); metaObject.addAll(list); } else if (targetType != null && targetType.isArray()) { Class<?> arrayComponentType = targetType.getComponentType(); Object array = Array.newInstance(arrayComponentType, list.size()); if (arrayComponentType.isPrimitive()) { for (int i = 0; i < list.size(); i++) { Array.set(array, i, list.get(i)); } value = array; } else { value = list.toArray((Object[]) array); } } else { if (list != null && list.size() > 1) { throw new ExecutorException("Statement returned more than one row, where no more than one was expected."); } else if (list != null && list.size() == 1) { value = list.get(0); } } return value; } ResultExtractor(Configuration configuration, ObjectFactory objectFactory); Object extractObjectFromList(List<Object> list, Class<?> targetType);  }### Answer:
@Test void shouldExtractNullForNullTargetType() { final Object result = resultExtractor.extractObjectFromList(null, null); assertThat(result).isNull(); }
@Test void shouldExtractList() { final List<Object> list = Arrays.asList(1, 2, 3); final Object result = resultExtractor.extractObjectFromList(list, List.class); assertThat(result).isInstanceOf(List.class); final List resultList = (List) result; assertThat(resultList).isEqualTo(list); }
@Test void shouldExtractArray() { final List<Object> list = Arrays.asList(1, 2, 3); final Object result = resultExtractor.extractObjectFromList(list, Integer[].class); assertThat(result).isInstanceOf(Integer[].class); final Integer[] resultArray = (Integer[]) result; assertThat(resultArray).isEqualTo(new Integer[]{1, 2, 3}); }
@Test void shouldExtractSet() { final List<Object> list = Arrays.asList(1, 2, 3); final Class<Set> targetType = Set.class; final Set set = new HashSet(); final MetaObject metaObject = mock(MetaObject.class); when(objectFactory.isCollection(targetType)).thenReturn(true); when(objectFactory.create(targetType)).thenReturn(set); when(configuration.newMetaObject(set)).thenReturn(metaObject); final Set result = (Set) resultExtractor.extractObjectFromList(list, targetType); assertThat(result).isSameAs(set); verify(metaObject).addAll(list); }
@Test void shouldExtractSingleObject() { final List<Object> list = Collections.singletonList("single object"); assertThat((String) resultExtractor.extractObjectFromList(list, String.class)).isEqualTo("single object"); assertThat((String) resultExtractor.extractObjectFromList(list, null)).isEqualTo("single object"); assertThat((String) resultExtractor.extractObjectFromList(list, Integer.class)).isEqualTo("single object"); }
@Test void shouldFailWhenMutipleItemsInList() { final List<Object> list = Arrays.asList("first object", "second object"); Assertions.assertThrows(ExecutorException.class, () -> resultExtractor.extractObjectFromList(list, String.class)); }