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 9081093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:13:55+00:00 2026-06-16T20:13:55+00:00

I have an application where I want to write unit tests to test the

  • 0

I have an application where I want to write unit tests to test the output, written to System.out (and perhaps System.err).
Each individual test works as expected, however when adding multiple tests in the same class some tests fail because JUnit4 appears to be multi-threaded (thus no guarantees exists on when exactly the stream is reset).
The same happens when I separate all test methods in their own class and use a test suite.

Any ideas?

private static final PrintStream SYS_OUT = System.out;
private static final PrintStream SYS_ERR = System.err;

private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

@Before
public final void setUpStreams() throws UnsupportedEncodingException {
    System.setOut(new PrintStream(this.outContent, true, CHARSET));
    System.setErr(new PrintStream(this.errContent, true, CHARSET));
}

@After
public final void cleanUpStreams() {
    System.setOut(SYS_OUT);
    System.setErr(SYS_ERR);
}

@Test
public final void listServicesAndMethods() throws ServiceException, UnsupportedEncodingException {
    com.example.Main.main(new String[]{"--list-services"});
    LOG.debug(this.outContent.toString(CHARSET));
    assertTrue("String not found", this.outContent.toString(CHARSET).contains("Some string"));
    assertFalse("Other string found", this.outContent.toString(CHARSET).contains("Some other string"));
    this.outContent.reset();
    this.errContent.reset();
}

Edit: It turns out that the issue with the failing tests was not (just?) because of the streams, but due to the fact that I stored the options in static fields in my main class. This has the effect that several options stay active during consecutive tests. I realised that after implementing Arian’s suggestion, I then used the second class as an instance, instead of calling static methods, thus solving my issue.

Thanks to all who replied.

  • 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-16T20:13:56+00:00Added an answer on June 16, 2026 at 8:13 pm

    If not already done so, rewrite the parts of your application to take the output as a parameter, instead of writing directly to System.out. This is usually better design regardless of testing.

    In each test, create a new output stream (or spy on System.out if you must) and pass it to the code unit under test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a application which runs in the background. I want to write
I have written a Windows Forms application and now I want to write some
I want to write a web application and want to use Ruby. I have
I have created blank Asp.Net-MVC 3 web application and want to write my own
I have a console application in which I want to write the name of
_Hey, I have a question. I want to write application which will have multiple
I want to write a few unit tests that do not make any changes
I want to refactor my model so I can properly write a unit test
I'm trying to write some tests for an MVC application we're developing. We have
I'm writing up a set of unit-tests for my MVC4 application. I want to

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.