AbstractFileLineIterator implements
                                              FileLineIterator<T> { @Override public boolean hasNext() { try { reader.mark(1); if (reader.read() != -1) { return true; } } catch (IOException e) { throw new FileException("Processing of reader was failed.", e, fileName); } finally { try { reader.reset(); } catch (IOException e) { throw new FileException("Processing of reader#reset was failed.", e, fileName); } } return false; } AbstractFileLineIterator(String fileName, Class<T> clazz,
            Map<String, ColumnParser> columnParserMap); @Override boolean hasNext(); @Override T next(); @Override void remove(); @Override void closeFile(); @Override List<String> getHeader(); @Override List<String> getTrailer(); @Override void skip(int skipLines); int getCurrentLineCount();  }
@Test public void testHasNext01() throws Exception { URL url = this.getClass().getResource( "AbstractFileLineIterator_hasNext01.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("int", new IntColumnParser()); columnParserMap.put("java.lang.String", new NullColumnParser()); AbstractFileLineIteratorImpl01<AbstractFileLineIterator_Stub07> fileLineIterator = new AbstractFileLineIteratorImpl01<AbstractFileLineIterator_Stub07>( fileName, AbstractFileLineIterator_Stub07.class, columnParserMap); BufferedReader mockReader = Mockito.mock(BufferedReader.class); Mockito.doNothing().when(mockReader).mark(1); Mockito.doReturn(0).when(mockReader).read(); ReflectionTestUtils.setField(fileLineIterator, "reader", mockReader); boolean result = fileLineIterator.hasNext(); assertTrue(result); Mockito.verify(mockReader).read(); }
@Test public void testHasNext02() throws Exception { URL url = this.getClass().getResource( "AbstractFileLineIterator_hasNext02.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("int", new IntColumnParser()); columnParserMap.put("java.lang.String", new NullColumnParser()); AbstractFileLineIteratorImpl01<AbstractFileLineIterator_Stub07> fileLineIterator = new AbstractFileLineIteratorImpl01<AbstractFileLineIterator_Stub07>( fileName, AbstractFileLineIterator_Stub07.class, columnParserMap); BufferedReader mockReader = Mockito.mock(BufferedReader.class); Mockito.doNothing().when(mockReader).mark(1); Mockito.doReturn(-1).when(mockReader).read(); Mockito.doNothing().when(mockReader).reset(); ReflectionTestUtils.setField(fileLineIterator, "reader", mockReader); boolean result = fileLineIterator.hasNext(); assertFalse(result); Mockito.verify(mockReader).read(); }
@Test public void testBuildHeader06() throws Exception { URL url = this.getClass().getResource( "AbstractFileLineIterator_buildHeader01.txt"); String fileName = url.getPath(); Map<String, ColumnParser> columnParserMap = new HashMap<String, ColumnParser>(); columnParserMap.put("java.lang.String", new NullColumnParser()); AbstractFileLineIteratorImpl02<AbstractFileLineIterator_Stub61> fileLineIterator = Mockito.spy( new AbstractFileLineIteratorImpl02<AbstractFileLineIterator_Stub61>( fileName, AbstractFileLineIterator_Stub61.class, columnParserMap)); Method method = AbstractFileLineIterator.class.getDeclaredMethod("buildLineReader"); method.setAccessible(true); method.invoke(fileLineIterator); FileException exception = new FileException("readLineからの例外です"); Mockito.doThrow(exception).when(fileLineIterator).hasNext(); try { method = AbstractFileLineIterator.class.getDeclaredMethod("buildHeader"); method.setAccessible(true); method.invoke(fileLineIterator); fail("FileExceptionがスローされませんでした"); } catch (InvocationTargetException e) { assertSame(exception, e.getTargetException()); } }