### Question:
ResourceCodeWriter extends CodeWriter { @Override public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { FileObject resource = filer.createResource(StandardLocation.SOURCE_OUTPUT, pkg.name(), fileName); return resource.openOutputStream(); } ResourceCodeWriter(Filer filer); @Override OutputStream openBinary(JPackage pkg, String fileName); @Override void close();  }### Answer:
@Test public void usesFilerToOpenStream() throws IOException { Filer filer = mock(Filer.class); FileObject fileObject = mock(FileObject.class); when(filer.createResource(Mockito.<Location> any(), anyString(), anyString())).thenReturn(fileObject); OutputStream expectedOS = mock(OutputStream.class); when(fileObject.openOutputStream()).thenReturn(expectedOS); ResourceCodeWriter resourceCodeWriter = new ResourceCodeWriter(filer); JPackage jPackage = new JCodeModel()._package("some.package"); OutputStream resultingOS = resourceCodeWriter.openBinary(jPackage, null); assertSame(expectedOS, resultingOS); }
@Test public void multipleCallsAreDelegatedToFiler() throws IOException{ Filer filer = mock(Filer.class); FileObject fileObject = mock(FileObject.class); when(filer.createResource(Mockito.<Location> any(), anyString(), anyString())).thenReturn(fileObject); ResourceCodeWriter resourceCodeWriter = new ResourceCodeWriter(filer); JPackage jPackage = new JCodeModel()._package("some.package"); resourceCodeWriter.openBinary(jPackage, null); resourceCodeWriter.openBinary(jPackage, null); verify(filer, times(2)).createResource(Mockito.<Location> any(), anyString(), anyString()); }