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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:05:12+00:00 2026-06-05T04:05:12+00:00

I have a class where Runtime.getRuntime() is used for executing a script from command

  • 0

I have a class where Runtime.getRuntime() is used for executing a script from command line and getting the result for further processing.

But when I write JUnit for this class, I cannot find a way to mock/avoid this Runtime.getRuntime().exec() .

I cannot use EasyMock or PowerMock or any other mock api other than Mockito.

Please give me a way to overcome this problem, because this is affecting the code coverage.

  • 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-05T04:05:14+00:00Added an answer on June 5, 2026 at 4:05 am

    You must refactor. Extract Runtime.getRuntime().exec() into a separate class:

    public class Shell {
    
      public Process exec(String command) {
        return Runtime.getRuntime().exec(command);
      }
    
    }
    

    Now insted of calling getRuntime() explicitly inject Shell class somehow to your class under test:

    public class Foo {
    
      private final Shell shell;
    
      public Foo(Shell shell) {
        this.shell = shell;
      }
    
      //...
      shell.exec(...)
    
    }
    

    In JUnit test simply inject mocked Shell class by passing it to constructor:

    @Mock
    private Shell shellMock;
    
    new Foo(shellMock);
    

    Sidenote: yes, I am not creating a Shell interface with one implementation. Do you mind? Mockito is not. Bonus: you can now verify if the correct process was called:

    verify(shellMock).exec("/usr/bin/gimp");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code down and this working fine from command line ... But
I have been curious about dynamically create class at runtime in C# and stumbled
I am getting warning: Instance method 'method' is being used on 'Class' which is
I have a Java Runtime.getRuntime().exec() problem. I run my java app on Linux and
Is possible create (register) a new class in runtime using delphi. I have a
I have class Question(db.Model): wording = db.StringProperty(required=True) class Answer(db.Model): question = db.ReferenceProperty(Question, required=True) userId
I have class to create user and return the info of user (success). class
I have: class BASE{ public: virtual void func1() = 0; }; Then I have
I have class Building in which i have one class member NSMutableArray *subnode.I have
I have class Question < ActiveRecord::Base has_many :tasks, :as => :task end class QuestionPlayer

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.