0

I have two processes - FileWriter and FileReader. FileWriter has a write() method which creates the file if it's missing and writes to the file. FileReader has a read() method which reads from the file and throws an CustomException if the file is not available.

During the normal application run, FileWriter.write() method is executed first followed by FileReader.read() method. Since the file will be always present, read() method never throws my CustomException unless something went wrong with FileWriter.

I am writing junit testcases for both these classes. When testing both classes independent of each other, I found my custom exception is being thrown when file is not present. For a really convoluted reason, I want the testcase to be marked as success and execute the next test. To achieve this, I did the below:

@Test
public void testRead() throws CustomException {
    boolean assumeTestcasePassed = false;
    FileReader fileReader = new FileReader();
    String fileContent = null;
    try {
        fileContent = fileReader.read();
    } catch (CustomException e) {
        assumeTestcasePassed = true;
    }
    if(assumeTestcasePassed){
        assertTrue(true);
    } else {
        assertTrue("File is empty", fileContent != null);
    }

}

Is there a better way to achieve what I am doing here?

Sree Sake
  • 83
  • 1
  • 12
  • Why can't you mock it? – Pankaj Gadge Dec 19 '17 at 19:28
  • If I mock it, I can't read the actual contents in the file. – Sree Sake Dec 19 '17 at 19:30
  • Why not just instruct JUnit to skip this test for the time being? That way you won't have to mess around with fake results, and you'll get a clear indication (a count of skipped tests) to show you that there's still work to do before you can consider all of your tests complete. – Bobulous Dec 19 '17 at 20:23
  • Where is the file to be read located? Perhaps add a static test file to the test resources folder of the project. – Andrew S Dec 19 '17 at 20:40
  • Your test should be deterministic. Either the file is always there or it is never. What confidence do you gain from a green test if you don't know what was checked by the test? – Stefan Birkner Dec 20 '17 at 11:09

2 Answers2

1

If I understand correctly, you are expecting an exception to be thrown, just do this:

@Test
public void testRead() {
    try {
        assertTrue(new FileReader().read() != null);
    } catch (CustomException e) {
        // Test passes
    }
}

You can also annotate like this answer: How do you assert that a certain exception is thrown in JUnit 4 tests?

mikeb
  • 10,578
  • 7
  • 62
  • 120
  • I need to test the contents of the file. If the file itself is not available, then it is ok. If I use fail in try block, I can't check the contents. – Sree Sake Dec 19 '17 at 19:32
  • See my edit, if the file is able to be read without an exception, it asserts that it is not null. If the file content is null it fails, but if it throws an exception it passes. – mikeb Dec 19 '17 at 20:04
1

You can expect the tested code to throw an exception in the @Test annotation:

@Test(expected = CustomException.class)
public void testRead() throws CustomException {
    FileReader fileReader = new FileReader();
    fileContent = fileReader.read();
}
pablochan
  • 5,625
  • 27
  • 42
  • That is my other testcase when I know for sure the file won't exist. But if I wouldn't know ahead if the file exists, what can I do? – Sree Sake Dec 19 '17 at 19:33
  • I'm not sure I understand. How can you not know that during tests? – pablochan Dec 19 '17 at 19:35
  • Let's say I have a test suite which executes all the tests in specified order. Something went wrong with FileWriter.write() method and the file wasn't created. Then my testcase for read() method shouldn't fail. PS: Actual scenario I'm working on is pretty complicated. But I gave the above example as a gist. – Sree Sake Dec 19 '17 at 19:38
  • Tests should be independent of each other. Either have a file ready or run `FileWriter` in your example. – pablochan Dec 19 '17 at 19:41