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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:29:38+00:00 2026-06-16T05:29:38+00:00

Consider the following code that uses JSch to create an SSH connection: public class

  • 0

Consider the following code that uses JSch to create an SSH connection:

public class DoSsh {
  private static final int DEFAULT_PORT = 22;

  public DoSsh(String user, String pass) {
    JSch jsch = new JSch();
    Session sess = jsch.getSession(user, pass, DEFAULT_PORT);
    ...

And the following test code that uses JMockit:

@Test
public void testDoShs() {
  // Change the default port
  Deencapsulation.setField(DoSsh.class, "DEFAULT_PORT", 2222);
  DoSsh ssh = new DoSsh("me","mypass");
  ...

The goal here is to cause the SSH connection to use an alternate port during test (2222 in this case) to connect to an in-memory SSH server (Apache MIRA).

When I debug this, I can see that the value of ‘DEFAULT_PORT’ has indeed been changed (thank you JMockit 🙂 The problem is that compiler has already optimized the call to ‘jsch.getSession’ and hard-coded the original value of 22 into it. So when I step into that call in the debugger, even though the value being passed in is 2222, the value inside the call is 22.

My question is, can anyone suggest a way to solve this that doesn’t involve making DEFAULT_PORT non-final?

  • 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-16T05:29:39+00:00Added an answer on June 16, 2026 at 5:29 am

    Found my own answer. It involves mocking out the call to ‘jsch.getSession’, but then calling the real version from within the mock, with the desired port number. This is basically an AOP approach. Deencapsulation is not used. Here’s the code:

    @MockClass(realClass = JSch.class)
    public static class MockedJSch {
        public JSch it;
        @Mock(reentrant = true)
        public Session getSession(final String user, final String pass, final int port) throws JSchException {
            return it.getSession(user, pass, TESTING_PORT);
        }
    }
    
    @BeforeMethod
    public void beforeMethod() {
      Mockit.setUpMocks(MockedJSch.class);
    }
    

    There are two key points to note here.

    1. The mocked method is marked as ‘reentrant’.
    2. The mock has a public instance member called “it” that is used to call the “real” method. That instance member is initialized somewhere in the bowels of JMockit to refer to the instance upon which this method is invoked, and that reference has access to the “real” version of the method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: using System.IO; using System.Threading.Tasks; public class MyClass { private int
Consider the following code sample that uses MEF to create an object of type
Consider the following code: class Program { static void Main(string[] args) { try {
Consider the following example code: class Foo { }; class Bar : public Foo
Consider the following two blocks of Java psedo-code in a system that uses optimistic
Consider the following code and assume that list is an synchronized List. List list
Consider the following code. It's an anchor tag with an id called leader-module-total that
Consider the following code: int i = 3 << 65; I would expect that
Consider the following code: while(true) { someFunction(); Thread.sleep(1000); } What I want is that,
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {

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.