Search code examples
jmockit

Compiler optimization causes original static final value to be used even when it's changed by JMockit


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?


Solution

  • 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.