title: Getting a file from current package
body: <p>This code runs well in my locally but gives file not found exception when we build using bamboo. Any idea/workaround?</p>
  
  <pre><code>final static String FILE_NAME ="/src/test/java/com/statement/SamplePDFStatementFile.txt";
  file = new File(FILE_NAME);
  FileInputStream fis = new FileInputStream(file);
  </code></pre>
  
  <p>I basically want to test a class which reads a file.  Here is the Complete Code.</p>
  
  <pre><code>//Main Class
  public class PdfRenderer {
  
      public void render(PdfFile pdfFile) throws IOException {
          FileInputStream fis = new FileInputStream(pdfFile.getFile());
  
          final HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
          response.setHeader("Content-Disposition", " attachment; filename=" + pdfFile.getAttachmentName());
  
          byte[] buff = new byte[2048];
          int bytesRead;
          while (-1 != (bytesRead = fis.read(buff, 0, buff.length))) {
              response.getOutputStream().write(buff, 0, bytesRead);
          }
  
          response.getOutputStream().flush();
  
      }
  }
  
  //Test
  
  public class PdfRendererTest{
  
      PdfFile pdfFile;
      File file;
  
      @Test
      public void test_DetailStatementsByAccountNumber() throws Exception {
          new AbstractSeamTest.ComponentTest() {
  
              PdfRenderer actionBean = new PdfRenderer();
  
              URL url = this.getClass().getResource("/SamplePDFStatementFile.txt");
              final String FILE_NAME = url.getFile();
  
              protected void testComponents() throws Exception {
  
                  file = new File(FILE_NAME);
  
                  context.checking(new Expectations() {{
                      one(pdfFile).getFile();
                      will(returnValue(file));
                      one(pdfFile).getAttachmentName();
                      will(returnValue(file.getName()));
                  }});
  
                  actionBean.render(pdfFile);
              }
  
          }.run();
      }
  }
  </code></pre>
  
  <p>I want to set an expectation that pdfFile.getFile() returns SamplePDFStatementFile.txt. If I use getResourceAsStream, am not sure how to convert it to a File Object.</p>
  
  <p>OK.. so now I am using. Looks like thats the answer :)</p>
  
  <pre><code>public void inputStreamToFile() throws Exception{
                  InputStream inputStream =  this.getClass().getResourceAsStream("/SamplePDFStatementFile.txt");
  
                  file = File.createTempFile("abc","def");
                  OutputStream out = new FileOutputStream(file);
  
                  int length = 0;
                  byte[] bytes = new byte[1024];
  
                  while ((length = inputStream.read(bytes)) != -1) {
                         out.write(bytes, 0, length);
                  }
  
                  inputStream.close();
                  out.flush();
                  out.close();
              }
  </code></pre>