title: How to mock connection for SFTP Connection
body: <p>My program opens sftp connection and connects to the server to get a file which is then processed.
  I am writing a test case for this method, trying to mock the connection.</p>
  
  <p>My actual class is:</p>
  
  <pre><code>public void getFile() {
      Session session = null;
      Channel channel = null;
      ChannelSftp channelSftp = null;
      String path = ftpurl;
      try {
          JSch jsch = new JSch();
          session = jsch.getSession(username, host, port);
          session.setConfig("StrictHostKeyChecking", "no");session.setPassword(JaspytPasswordEncryptor.getDecryptedString(jaspytEncryptionKey, jaspytEncryptionAlgorithm, password));
  
          session.connect();
          channel = session.openChannel("sftp");
          channel.connect();
          channelSftp = (ChannelSftp) channel;
  
          channelSftp.cd(path);
  
          Vector&lt;ChannelSftp.LsEntry&gt; list = channelSftp.ls("*.csv");
          for (ChannelSftp.LsEntry entry : list) {
              if (entry.getFilename().startsWith("A...")) {
                  findByFileName(entry.getFilename());
              }
          }
          channelSftp.exit();
          session.disconnect();
      } catch (JSchException e) {
          LOGGER.error("JSch exception"+e);
      } catch (SftpException e) {
          LOGGER.error("Sftp Exception"+e);
      }
  }
  </code></pre>
  
  <p>Test class so far:</p>
  
  <pre><code>@Test
  public void getNamesTestValid() throws IOException, JSchException { 
      JSch jsch = new JSch();
  
      Hashtable config = new Hashtable();
      config.put("StrictHostKeyChecking", "no");
      JSch.setConfig(config);
  
      Session session = jsch.getSession( "remote-username", "localhost", 22999);
      session.setPassword("remote-password");
  
      session.connect();
  
      Channel channel = session.openChannel( "sftp" );
      channel.connect();
  
      ChannelSftp sftpChannel = (ChannelSftp) channel;
          Mockito.when(fileRepository.findByFileName(fileName)).thenReturn(fileDetail);
      scheduler.getCSVFileNames();    
  }
  </code></pre>
  
  <p>When trying to mock the connection, it searches for the actual port and the error is port invalid, connection refused.
  I only want to mock the connection.
  My other doubt is after mocking the connection from where should i read the file details.</p>