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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:01:05+00:00 2026-05-10T20:01:05+00:00

I have a Java method which starts up a Process with ProcessBuilder, and pipes

  • 0

I have a Java method which starts up a Process with ProcessBuilder, and pipes its output into a byte array, and then returns its byte array when the process is finished.

Pseudo-code:

ProcessBuilder b = new ProcessBuilder('my.exe') Process p = b.start(); ... // get output from process, close process 

What would be the best way to go about unit testing this method? I haven’t found a way to mock ProcessBuilder (it’s final), even with the incredibly awesome JMockit, it gives me a NoClassDefFoundError:

java.lang.NoClassDefFoundError: test/MockProcessBuilder     at java.lang.ProcessBuilder.<init>(ProcessBuilder.java)     at mypackage.MyProcess.start(ReportReaderWrapperImpl.java:97)     at test.MyProcessTest.testStart(ReportReaderWrapperImplTest.java:28) 

Any thoughts?


Answer – As Olaf recommended, I ended up refactoring those lines to an interface

Process start(String param) throws IOException; 

I now pass an instance of this interface into the class I wanted to test (in its constructor), normally using a default implementation with the original lines. When I want to test I simply use a mock implementation of the interface. Works like a charm, though I do wonder if I’m over-interfacing here…

  • 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. 2026-05-10T20:01:06+00:00Added an answer on May 10, 2026 at 8:01 pm

    Shield yourself from the classes to be mocked. Create an interface either for doing what you really want (e.g. hiding the fact that external processes are involved at all) or only for Process and ProcessBuilder.

    You don’t want to test, that ProcessBuilder and Process work, only that you can work with their output. When you create an interface one trivial implementation (that can be inspected easily) delegates to ProcessBuilder and Process, another implementation mocks this behaviour. Later on you might even have another implementation that does what you need without starting another process.

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

Sidebar

Ask A Question

Stats

  • Questions 160k
  • Answers 160k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not sure if colorpicker is the right one for you. May 12, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer No, it is not possible. Because ASP.NET textbox finally turns… May 12, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer Here is literally translated query to TSQL 2000. Although, it… May 12, 2026 at 11:38 am

Related Questions

I'm starting a Process from a Java program. I hold onto it and at
I have a method that takes an array of queries, and I need to
I'm in the process of developing a multi-tiered financial processing application in Java using
I'm developing a fair sized hospital simulation game in java. Right now, my pain

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.