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

  • Home
  • SEARCH
  • 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 9245933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:22:33+00:00 2026-06-18T09:22:33+00:00

I’ve got a few test methods that aren’t behaving as I expect them to

  • 0

I’ve got a few test methods that aren’t behaving as I expect them to be. In these scenarios, I’m writing logging messages to a text file.

Both methods are about closing the output link to the file and attempting to write to it. Obviously this means I expect an IOException which is also reflected in my code.

These are the two test methods I’m talking about

@Test(expected = IOException.class)
public void writeToClosedConnection() throws IOException {
    LogHandler.getInstance().createNewChatlog("Jack");
    LogHandler.getInstance().stopLogging(); // Closes the bufferedWriter
    LogHandler.getInstance().writeToChatLog(new Message("Jim", "CAN'T WORK"));
}

@Test(expected = IOException.class)
public void closeStream() throws IOException {
    log = new ChatLog(new GregorianCalendar(), "STANDARDTESTLOG", directory);
    log.closeLogFile(); // Closes the bufferedWriter
    log.addMessage(new Message("Jim", "CAN'T WORK"));
}

Both methods to write messages belong in the same route. writeToChatLog invokes addMessage, which will in turn call on the writeToLogFile method.

This last method is defined as

protected void writeToLogFile(String message) {
    try {
        if (logging) {
            bWriter.write(message);
            bWriter.newLine();
            bWriter.flush();
        } else {
            throw new ForbiddenActionException("Logging is disabled");
        }
    } catch (IOException e) {
        OutputUtil.showErrorMessage("Couldn't write to logfile", "IO error");
        e.printStackTrace();
    }
}

The issue at hand is that even though IO errors are thrown (twice this:)

java.io.IOException: Stream closed
    at java.io.BufferedWriter.ensureOpen(Unknown Source)
    at java.io.BufferedWriter.write(Unknown Source)
    at java.io.Writer.write(Unknown Source)
    at io.Log.writeToLogFile(Log.java:41)
    at io.ChatLog.addMessage(ChatLog.java:16)
    at tests.ChatLogTest.closeStream(ChatLogTest.java:76)

and the popup message (Couldn't write to logfile) is shown, I still get an assertionError (java.lang.AssertionError: Expected exception: java.io.IOException).

JUnit is version 4.

Why is this happening?

  • 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-06-18T09:22:35+00:00Added an answer on June 18, 2026 at 9:22 am

    You never really throw IOException. Inside writeToLogFile you catch it, log it and do nothing else. From you outside world perspective nothing wrong happened:

    protected void writeToLogFile(String message) {
        try {
            //...
        } catch (IOException e) {
            //...
            e.printStackTrace();
        }
    }
    

    You see, even if IOException was thrown, it’s suppressed. Thus it never escapes out of writeToClosedConnection() method so JUnit can’t see it. And it fails the test. A quick solution would be to propagate the exception, which unfortunately requires modifying your signature:

    protected void writeToLogFile(String message) throws IOException {
        try {
            //...
        } catch (IOException e) {
            //...
            throw e;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
i got an object with contents of html markup in it, for example: string
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

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.