title: Unit testing FTP in Java (JSch & MockFtpServer)
body: <p>I'm using JCraft's JSch library for as a FTP client for my program, it's working fine. But I had difficulties creating a mock (MockFtpServer) for unit testing. </p>
  
  <pre><code>import com.jcraft.jsch.Channel;
  import com.jcraft.jsch.ChannelSftp;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.Session;
  
  import org.junit.AfterClass;
  import org.junit.BeforeClass;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  
  import org.mockftpserver.fake.FakeFtpServer;
  import org.mockftpserver.fake.UserAccount;
  import org.mockftpserver.fake.filesystem.FileEntry;
  import org.mockftpserver.fake.filesystem.FileSystem;
  import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit4.SpringRunner;
  
  import java.util.Properties;
  
  @RunWith(SpringRunner.class)
  @SpringBootTest
  public class SFTPManagerTest {
  
  static FakeFtpServer fakeFtpServer;
  
  @BeforeClass
  public static void startFTP() throws Exception {
      fakeFtpServer = new FakeFtpServer();
      fakeFtpServer.setServerControlPort(9999);
  
      FileSystem fileSystem = new UnixFakeFileSystem();
      fileSystem.add(new FileEntry("/data/JFS_HCID_APPLICATION_STATUS_20190109.TXT", "&lt;schema/&gt;"));
      fakeFtpServer.setFileSystem(fileSystem);
  
      UserAccount userAccount = new UserAccount("user", "password", "/");
      fakeFtpServer.addUserAccount(userAccount);
      fakeFtpServer.start();
  }
  
  @Test
  public void getFileListing() {
  
      try {
          JSch jsch = new JSch();
          Session jschSession = jsch.getSession("user", "localhost", 9999);
          jschSession.setPassword("password");
  
          Properties config = new Properties();
          config.put("StrictHostKeyChecking", "no");
          jschSession.setConfig(config);
          jschSession.connect();
  
          Channel jschChannel = jschSession.openChannel("ftp");
          jschChannel.connect();
          ChannelSftp channel = (ChannelSftp) jschChannel;
          Vector v = channel.ls("/data");
          Assert.assertNotNull(v);
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  
  @AfterClass
  public static void stopFTP() {
      fakeFtpServer.stop();
  }
  }
  </code></pre>
  
  <p>Dependencies:</p>
  
  <ul>
  <li>com.jcraft:jsch:0.1.55</li>
  <li>org.mockftpserver:MockFtpServer:2.7.1</li>
  </ul>
  
  <p>Output: it always stuck/stall during executing test method, not finishing. When stuck/stalling I could access the MockFtpServer with WinSCP or FTP CLI. From the log it seems the command from JSch not recognized.</p>
  
  <pre class="lang-none prettyprint-override"><code>2019-02-18 17:54:15,996 INFO  [Thread-0] org.mockftpserver.core.server.AbstractFtpServer: Connection accepted from host /127.0.0.1
  2019-02-18 17:54:16,002 INFO  [Thread-6] org.mockftpserver.core.command.AbstractTrackingCommandHandler: Sending reply [220 Service ready for new user. (MockFtpServer 2.7.1; see http://mockftpserver.sourceforge.net)]
  2019-02-18 17:54:16,004 INFO  [Thread-6] org.mockftpserver.core.session.DefaultSession: Received command: [SSH-2.0-JSCH-0.1.54]
  2019-02-18 17:54:16,004 WARN  [Thread-6] org.mockftpserver.core.command.UnsupportedCommandHandler: No CommandHandler is defined for command [SSH-2.0-JSCH-0.1.54]
  2019-02-18 17:54:16,005 INFO  [Thread-6] org.mockftpserver.core.command.AbstractTrackingCommandHandler: Sending reply [502 Command not implemented: SSH-2.0-JSCH-0.1.54.]
  </code></pre>
  
  <p>Where did i go wrong?</p>