How to work around jsch.addIdentity() in Junit test?
<p>I use apache mina sshd to set up a mocked ssh server for Junit testing purpose. Since the documentation for apache mina is quite unclear, I cannot figure out how to deal with authentication problem in testing.</p>

<p>Code I would like to test, basically using Jsch to transfer file from local to remote.</p>

<pre><code>public static void scpTo(String rfilePath, String lfilePath, String user, String host, String keyPath, int port) {
    FileInputStream fis=null;
    try {
        while(true) {
            String rfile = rfilePath;
            String lfile = lfilePath;

            JSch jsch = new JSch();
            jsch.addIdentity(keyPath);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");

            Session session = jsch.getSession(user, host, port);
            session.setConfig(config);

            session.connect();

            // exec 'scp -t rfile' remotely
            String command = "scp " + " -t " + rfile;
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            // get I/O streams for remote scp
            OutputStream out = channel.getOutputStream();
            InputStream in = channel.getInputStream();

            channel.connect();

            if (checkAck(in) != 0) {
                System.exit(0);
            }

            File _lfile = new File(lfile);
            if(!_lfile.exists()) {
                System.err.println("Local file not existing");
            }

            //check file existing
            File _rfile = new File(rfile);
            if (_rfile.exists()) {
                System.out.println("Remote file already existed");
                break;
            }

            // send "C0644 filesize filename", where filename should not include '/'
            long filesize = _lfile.length();
            command = "C0644 " + filesize + " ";
            if (lfile.lastIndexOf('/') &gt; 0) {
                command += lfile.substring(lfile.lastIndexOf('/') + 1);
            } else {
                command += lfile;
            }
            command += "\n";
            out.write(command.getBytes());
            out.flush();
            if (checkAck(in) != 0) {
                System.exit(0);
            }

            // send a content of lfile
            fis = new FileInputStream(lfile);
            byte[] buf = new byte[1024];
            while (true) {
                int len = fis.read(buf, 0, buf.length);
                if (len &lt;= 0) break;
                out.write(buf, 0, len); //out.flush();
            }
            fis.close();
            fis = null;
            // send '\0'
            buf[0] = 0;
            out.write(buf, 0, 1);
            out.flush();
            if (checkAck(in) != 0) {
                System.exit(0);
            }
            out.close();

            channel.disconnect();
            session.disconnect();

            //System.exit(0);

            Thread.sleep(2000);
        }
    } catch (Exception e) {
        System.out.println(e);
        try {
            if (fis != null) fis.close();
        } catch (Exception ee) {
        }
    }
}
</code></pre>

<p>And the setUp used for testing. If I would like to authenticate all the user &amp; host pairs by using a given key file in my resource folder, what should I do for setUp? For the current setUp, it will have Auth fail error.</p>

<pre><code>@Before
public void setup() {
    user = "siyangli";
    host = "localhost";
    //pick a port not occupied for tesing
    port = 22998;
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(port);
    keyPath = "/Users/siyangli/.ssh/id_rsa";
    //it will change the key file??? do not know why, how to work around the authentication key??
    sshd.setKeyPairProvider(new FileKeyPairProvider(new String[]{"/Users/siyangli/.ssh/id_rsa"}));
    //sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(keyPath));
    sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
        public boolean authenticate(String username, String password, ServerSession session) {
            return true;
        }
    });
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() {
        @Override
        public boolean authenticate(String username, PublicKey key, ServerSession session) {
            return true;
        }
    });
    CommandFactory myCommandFactory = new CommandFactory() {
        public Command createCommand(String command) {
            System.out.println("Command: " + command);
            return null;
        }
    };
    sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));
    List&lt;NamedFactory&lt;UserAuth&gt;&gt; userAuthFactories = new ArrayList&lt;NamedFactory&lt;UserAuth&gt;&gt;();
    userAuthFactories.add(new UserAuthPassword.Factory());
    sshd.setUserAuthFactories(userAuthFactories);
    List&lt;NamedFactory&lt;Command&gt;&gt; namedFactoryList = new ArrayList&lt;NamedFactory&lt;Command&gt;&gt;();
    namedFactoryList.add(new SftpSubsystem.Factory()); sshd.setSubsystemFactories(namedFactoryList);
    try {
        sshd.start();
    } catch (IOException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    System.out.println("Finished setup !!! ");
}
</code></pre>