【发布时间】:2019-08-20 05:35:57
【问题描述】:
我编写了一个 Java 类,在其中尝试访问 FTP。
我在 Eclipse 上工作,我想对此进行 Junit 测试。我知道如何测试公共类,但我坚持测试静态 void main 方法。
这是我的 ftp.java 类:
public class ftp {
public static void main(String[] args) {
FTPClient client = new FTPClient();
try {
client.connect("host");
// Try to login and return the respective boolean value
boolean login = client.login("login", "pass");
// If login is true notify user
if (login) {
System.out.println("Connection established...");
// Try to logout and return the respective boolean value
boolean logout = client.logout();
// If logout is true notify user
if (logout) {
System.out.println("Connection close...");
}
// Notify user for failure
} else {
System.out.println("Connection fail...");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// close connection
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我开始这样创建 ftpTest.java :
public class ftpTest {
ftp testaccess = new ftp();
FTPClient testclient = ftp.client;
@Test
public void testftp() {
fail("Not yet implemented");
}
}
任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
我已经找到了那个帖子,但我无法用它来解决我的问题。更重要的是,FTP 连接看起来非常具体。
-
啊哈,与其测试 main 函数,不如测试 main 中给出的代码。那是另一种野兽。就像触发那些异常一样。