mil.dod.th.ose.test/src/mil/dod/th/ose/test/FileReaderMocker.java
public static FileReader mockFileReader(String contents)
    {
        FileReader fileReader = mock(FileReader.class);
        
        // mock the bytes retrieved
        final InputStream inputStream = new ByteArrayInputStream(contents.getBytes());
        try
        {
            when(fileReader.read(Mockito.any(char[].class), anyInt(), anyInt())).thenAnswer(new Answer<Integer>()
            {
                @Override
                public Integer answer(InvocationOnMock invocation) throws Throwable
                {
                    char[] cbuf = (char[])invocation.getArguments()[0];
                    byte[] bbuf = new byte[cbuf.length];
                    int rv = inputStream.read(bbuf, (Integer)invocation.getArguments()[1], 
                            (Integer)invocation.getArguments()[2]);
                    System.arraycopy(new String(bbuf).toCharArray(), 0, cbuf, 0, bbuf.length);
                    return rv;
                }
            });
        }
        catch (IOException e)
        {
            throw new IllegalStateException(e);
        }
        
        return fileReader;
    }
/**
     * Mock a file reader that will return the given content.
     */
Mock a file reader that will return the given content.
Mock a file reader that will return the given content .
public static FileReader mockFileReader(String contents)
    {
        FileReader fileReader = mock(FileReader.class);
        final InputStream inputStream = new ByteArrayInputStream(contents.getBytes());
        try
        {
            when(fileReader.read(Mockito.any(char[].class), anyInt(), anyInt())).thenAnswer(new Answer<Integer>()
            {
                @Override
                public Integer answer(InvocationOnMock invocation) throws Throwable
                {
                    char[] cbuf = (char[])invocation.getArguments()[0];
                    byte[] bbuf = new byte[cbuf.length];
                    int rv = inputStream.read(bbuf, (Integer)invocation.getArguments()[1], 
                            (Integer)invocation.getArguments()[2]);
                    System.arraycopy(new String(bbuf).toCharArray(), 0, cbuf, 0, bbuf.length);
                    return rv;
                }
            });
        }
        catch (IOException e)
        {
            throw new IllegalStateException(e);
        }
        return fileReader;
    }
public static FileReader mockFileReader ( String contents ) { FileReader fileReader = mock ( FileReader . class ) ; final InputStream inputStream = new ByteArrayInputStream ( contents . getBytes ( ) ) ; try { when ( fileReader . read ( Mockito . any ( char [ ] . class ) , anyInt ( ) , anyInt ( ) ) ) . thenAnswer ( new Answer < Integer > ( ) { @ Override public Integer answer ( InvocationOnMock invocation ) throws Throwable { char [ ] cbuf = ( char [ ] ) invocation . getArguments ( ) [ 0 ] ; byte [ ] bbuf = new byte [ cbuf . length ] ; int rv = inputStream . read ( bbuf , ( Integer ) invocation . getArguments ( ) [ 1 ] , ( Integer ) invocation . getArguments ( ) [ 2 ] ) ; System . arraycopy ( new String ( bbuf ) . toCharArray ( ) , 0 , cbuf , 0 , bbuf . length ) ; return rv ; } } ) ; } catch ( IOException e ) { throw new IllegalStateException ( e ) ; } return fileReader ; }
Mock a file reader that will return the given content.
Mock a file reader that will return the given content .
// mock the bytes retrieved