Java中的FTP单元测试(JSch和MockFtpServer)

Java中的FTP单元测试(JSch和MockFtpServer),java,ftp,mocking,jsch,Java,Ftp,Mocking,Jsch,我正在使用JCraft的JSch库作为我的程序的FTP客户端,它运行良好。但是我很难为单元测试创建一个mock(MockFtpServer) 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.Bef

我正在使用JCraft的JSch库作为我的程序的FTP客户端,它运行良好。但是我很难为单元测试创建一个mock(MockFtpServer)

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", "<schema/>"));
    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();
}
}

我哪里出错了?

JSch是一个SSH/SFTP库。您不能使用FTP服务器对其进行测试。您需要使用SFTP服务器。FTP和SFTP是两种完全不相关且不同的协议