Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7036705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:27:22+00:00 2026-05-28T01:27:22+00:00

While reviewing my code coverage i noticed a lot of Unit tests fail to

  • 0

While reviewing my code coverage i noticed a lot of Unit tests fail to check finally blocks which try to close open InputStreams in finally blocks.

One Example excerpt is:

  try {
      f = new BufferedInputStream(new FileInputStream(source));
      f.read(buffer);
  } finally {
      if (f != null)
          try {
              f.close();
          } catch (IOException ignored) {
          }
      }
  }

Is there any appropriate solution to check everything inside the finally block using JUnit4 ?

I know that a code coverage of 100% is not achievable while keeping maximum productivity in mind. However these red lines are sort of an eyecatcher in the report.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:27:22+00:00Added an answer on May 28, 2026 at 1:27 am

    First of all consider using IOUtils.closeQuietly(), which will reduce your untested code (and probably duplication) into:

      try {
          f = new BufferedInputStream(new FileInputStream(source));
          f.read(buffer);
      } finally {
          IOUtils.closeQuietly(f);
      }
    

    Now it becomes hard. The “right” way would be to externalize the creation of BufferedInputStream into another class and inject mock. Having a mock you can verify whether appropriate close() method was invoked.

    @JeffFoster‘s answer is pretty close to what I mean, however I would recommend composition over inheritance (at the expense of more code):

      try {
          f = fileSystem.open(source);
          f.read(buffer);
      } finally {
          IOUtils.closeQuietly(f);
      }
    

    Where fileSystem is an instance of FileSystem interface with simple real implementation injected in production code or mock for testing.

    interface FileSystem {
    
        InputStream open(String file);
    
    }
    

    Another benefit of externalizing file opening is that if you one decide to remove buffering or add encryption, there is just a single place to modify.

    Having that interface you instantiate your test code with mocks (using Mockito):

    //given
    FileSystem fileSystemMock = mock(FileSystem.class);
    InputStream streamMock = mock(InputStream.class);
    
    given(fileSystemMock.open("file.txt")).willReturn(streamMock);
    
    //when
    //your code
    
    //then
    verify(streamMock).close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sifting through some of my old bugs and while reviewing some nasty code
I'm having thread contention in an OLTP app. While reviewing the code involved, I
I'm reviewing some code for a colleague and while there's nothing inherently wrong with
While reviewing some of the code written in the Twitter Bootstrap Javascript, it looks
I am reviewing the c# code of an application and documenting. While going through
When reviewing, I sometimes encounter this kind of loop: i = begin while (
After some help and reviewing of the code I got this working without any
I am finishing a course on design patterns, and while reviewing the notes came
I'm reviewing some legacy code and I've found a bug that causes the response
Reviewing a quite old project I found the following curious code snippet (only relevant

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.