简体   繁体   English

FTP的junit测试用例

[英]junit test case for FTP

Can some one tell me how to write a junit test case for the following piece of code. 有人可以告诉我如何为下面的代码编写一个junit测试用例。 The one i have written doesn't get enter into the try body. 我写的那个没有进入try主体。 why..?? 为什么..??

code:- 码:-

public void dataImport ( String scheme , String dataSource , String savePath ) throws ImportException {
    LOG.debug("Entered");
    final InputStream inputStream = null ;

    try {
            String host = "localhost";
            String user = "user";
            String pass = "pass";
            String filePath ="/A/a1.txt";     ( this are actually extracted from dataSource )
            FTPClient ftpClient = new FTPClient();
          ftpClient.connect(host, 21);
          ftpClient.login(user, pass);
          inputStream = ftpClient.retrieveFileStream(filePath);

          //saving it to the file System

          }

    catch (final IOException ex) { 
          throw new ImportException(ex.getMessage(), ex);
          }

    finally {
          try {
            if (inputStream != null) {
              inputStream.close();
            }
          } catch (final IOException e) {
            throw new ImportException(e.getMessage(), e);
          }
        }
        }

Junit test case:- Junit测试用例:-

 @Test(expected=NullPointerException.class)
  public void testImportData () throws ImportException , IOException {
  FTPImporter fi = new FTPImporter();
  try{ 
  fi.dataImport("ftp" , "ftp://user:pass@localhost/A/a1.txt" , "desti" );
  fail("ere");
  }
  catch(ImportException e ){
  assertNotNull(e.getMessage());
 }
 }        

Are you sure FTPImporter constructor succeeds? 您确定FTPImporter构造函数成功吗?
Check your logs, debug testImportData method in IDE or wrap new FTPImporter() in try-catch block and log exception, something like this: 检查您的日志,在IDE中调试testImportData方法,或将new FTPImporter()包装在try-catch块中并记录异常,如下所示:

FTPImporter fi = null;
try {
    fi = new FTPImporter();
} catch (Exception e) {
    e.printStackTrace();
    fail("Error!");
}

Chance is your constructor throws NullPointerException which is expected by this test. 可能是您的构造函数抛出NullPointerException ,这是该测试所期望的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM