首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java (JSch & MockFtpServer)对FTP进行单元测试

用Java (JSch & MockFtpServer)对FTP进行单元测试
EN

Stack Overflow用户
提问于 2019-02-18 11:51:19
回答 1查看 2.8K关注 0票数 2

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

代码语言:javascript
复制
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();
}
}

依赖关系:

  • com.jcraft:jsch:0.1.55
  • org.mockftpserver:MockFtpServer:2.7.1

输出:在执行测试方法时,它总是卡住/停滞,而不是完成。当卡住/延迟时,我可以使用MockFtpServer或WinSCP访问。从日志中看,似乎无法识别来自JSch的命令。

代码语言:javascript
复制
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.]

我哪里出错了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-18 12:07:14

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54746688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档