0

有人可以告诉我如何为以下代码编写一个 junit 测试用例。我写的那个没有进入尝试正文。为什么..??

代码:-

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(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());
 }
 }        
4

1 回答 1

0

你确定FTPImporter构造函数成功了吗?检查您的日志,在 IDE中
调试方法或包装在 try-catch 块中并记录异常,如下所示:testImportDatanew FTPImporter()

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

机会是您的构造函数抛出NullPointerException,这是该测试所预期的。

于 2013-04-16T12:01:02.533 回答