### Question:
SourceCodeWriter extends CodeWriter { @Override public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { String qualifiedClassName = toQualifiedClassName(pkg, fileName); JavaFileObject sourceFile = filer.createSourceFile(qualifiedClassName); return sourceFile.openOutputStream(); } SourceCodeWriter(Filer filer); @Override OutputStream openBinary(JPackage pkg, String fileName); @Override void close();  }### Answer:
@Test public void usesFilerToOpenStream() throws IOException { Filer filer = mock(Filer.class); JavaFileObject fileObject = mock(JavaFileObject.class); when(filer.createSourceFile(anyString())).thenReturn(fileObject); OutputStream expectedOS = mock(OutputStream.class); when(fileObject.openOutputStream()).thenReturn(expectedOS); SourceCodeWriter sourceCodeWriter = new SourceCodeWriter(filer); JPackage jPackage = new JCodeModel()._package("some.package"); OutputStream resultingOS = sourceCodeWriter.openBinary(jPackage, "SomeClass.java"); assertSame(expectedOS, resultingOS); }
@Test public void multipleCallsAreDelegatedToFiler() throws IOException { Filer filer = mock(Filer.class); JavaFileObject fileObject = mock(JavaFileObject.class); when(filer.createSourceFile(anyString())).thenReturn(fileObject); SourceCodeWriter sourceCodeWriter = new SourceCodeWriter(filer); JPackage jPackage = new JCodeModel()._package("some.package"); sourceCodeWriter.openBinary(jPackage, "SomeClass.java"); sourceCodeWriter.openBinary(jPackage, "SomeClass.java"); verify(filer, times(2)).createSourceFile(anyString()); }