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