How to write unit test to cover the case when an IOException is thrown?

I have the following class:

    public class FileLoader {   

         private Map<Brand, String> termsOfUseText = new HashMap<Brand, String>();

         public void load() {
            for (Brand brand : Brand.values()) {
                readAndStoreTermsOfUseForBrand(brand);
            }
         }

         private void readAndStoreTermsOfUseForBrand(Brand brand) {
            String resourceName = "termsOfUse/" + brand.name().toLowerCase() + ".txt";          
            InputStream in = this.getClass().getClassLoader().getResourceAsStream(resourceName);            
            try {
                String content = IOUtils.toString(in);          
                termsOfUseText.put(brand, content);
            } catch (IOException e) {
                throw new IllegalStateException(String.format("Failed to find terms of use source file %s", resourceName),e);
            }
         }

        public String getTextForBrand(Brand brand) {
            return termsOfUseText.get(brand);
        }
    }

The brand is an enumeration, and I need all the valid .txt files to be in the classpath. How can I throw an IOException, given that the Brand enumeration contains all valid brands and all .txt files exist for them?

Suggestions for refactoring the current code are welcome if this makes it more verifiable!

+5
source share
4 answers

Three options that I see immediately:

  • PowerMock, IOUtils.toString(). , PowerMock - . - .
  • IOUtils . , IOException.
  • InputStream . , InputStream.
+3

NullPointerException, getResourceAsStream null, in==null. IOException . , , , , , . ?

0

I would use a layout to accomplish this.

Example (untested, just so you might think):

@Test(expected=IllegalStateException.class)
public void testThrowIOException() {
    PowerMockito.mockStatic(IOUtils.class);
    PowerMockito.when(IOUtils.toString()).thenThrow(
             new IOException("fake IOException"));
    FileLoader fileLoader = new FileLoader();
    Whitebox.invokeMethod(fileLoader, 
            "readAndStoreTermsOfUseForBrand", new Brand(...));
    // If IllegalStateException is not thrown then this test case fails (see "expected" above)
}
0
source

The code below is not fully tested.
To trigger the use of an IOException:

FileInputStream in = this.getClass().getClassLoader().getResourceAsStream(resourceName);
in.mark(0);  
//read some data
in.reset(); //IOException

To verify the use of an IOException case:

void test  
{  
    boolean success = false;  
    try  
    {  
      //code to force ioException  
    }  
    catch(IOException ioex)  
    {  
       success = true;  
    }  
    assertTrue(success);  
}

In JUnit4

@Test(expected=IOException.class)  
void test  
{  
   //code to force ioException  
}

Another JUnit

void test  
{  
   try  
   {  
     //code to force IOException  
     fail("If this gets hit IO did not occur, fail test");  
   }
   catch(IOException ioex)  
   {
    //success!
    }  
}
-1
source

All Articles