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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:18:39+00:00 2026-06-02T10:18:39+00:00

This post is similar to: this I have a console app that expects both

  • 0

This post is similar to: this

I have a console app that expects both user input and has responsive user output. I am writing some unit tests for it to make sure that the code works properly. I need to be able to make sure that the output and input for it are what I expect them to be.

Essentially, I have a main method being tested that asks for a file or input. In my test, I use System.setOut and System.setIn to set those to context.mock versions of PrintStream and InputStream objects respectively. I don’t care about what System.out.println calls get made until I have to test the actual processing of the program data, when it outputs its results to the console. So to summarize:

Here’s my source being tested (something close, I trimmed much out): http://ideone.com/rptC0

Here’s what I have in my mocking procedure: http://ideone.com/VkvqM

And here’s the exception that I’m getting: ideone.com/OEOS8

As you can clearly see in my expectations, I’m explicitly saying that I was planning to print the exact same string out, and I specified that in my expectations. But the exception says that it was unexpected. I don’t understand…

  • 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-02T10:18:47+00:00Added an answer on June 2, 2026 at 10:18 am

    JMock expects you to declare the Expectations using context.checking() then call the code under test, then call context.assertIsSatisfied() (although sometimes the last step is done implicitly if using an appropriate test runner).

    You seem to be immediately calling context.assertIsSatisfied() before running any code.

    Also, the code you posted uses the variable mn which does not appear to be defined – is that actually the code you are running? Or should that variable be mockIn instead?

    Updated: OK, the problem is probably that you are trying to mock a static method – JMock doesn’t support this – see jmock mocking a static method . See particularly the answer from Steve Freeman, who is one of the JMock authors.

    Updated 2: I would try something like this, setting an expectation in the @Before setup:

    @Before
    public void setMinimalMockingExpectations() throws IOException
    {
        oldIn = System.in;
        oldOut = System.out;
        pipe = new PipedOutputStream();
        testIn = new PipedInputStream(pipe);
        mockOut = context.mock(PrintStream.class);
        System.setOut(mockOut);
        System.setIn(testIn);
    
        expectQuestion();
    }
    
    private void expectQuestion()
    {
        Expectations exp = new Expectations()
        {
            {
                one(mockOut).println(main.QUESTION);
            }
        };
        context.checking(exp);
    
    }
    
    @After
    public void reset()
    {
        System.setIn(oldIn);
        System.setOut(oldOut);
    }
    
    @Test
    public void fileChoiceReturnsFalse() throws IOException
    {
        String FILE = "F\n";
        pipe.write(FILE.getBytes());
    
        assertFalse(main.promptStringOrFile());
    
        context.assertIsSatisfied(); // can avoid this call by using the right
                                        // test runner
    }
    

    and create two similar tests to check behaviour for input “I” and any other input (for which the question should be repeated once).

    The promptStringOrFile() method is probably clearer if you use BufferedReader.readLine() rather than worrying about chars.

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

Sidebar

Related Questions

I am doing a query that converts rows to columns similar to this post
Using the following steps: (I have checked this similar post , which does not
This post is similar to one I have posted before about a dropdown filtering
I have a similar challenge to this post: Batch insert/update with entity framework from
Following this post and the other posts that it links to, I have successfully
I have a similar problem to this post . I need to display up
I have a similar post on this on StackOverflow but perhaps my misunderstanding is
I have an error similar to the one in this post . Now, I'm
This post is very similar to my previous one, but the data structures are
Django code samples involving post data often shows code similar to this: if request.method

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.