2016-08-12 113 views
1

我正在用Mockito學習JUnit。我對如何開始以及如何爲下面的課程編寫單元測試有所瞭解。這將是很高興,如果有人能幫助我在寫他們:如何編寫低於類的junit測試用例

@Service 
public class GetDataFromHistDataImpl implements GetDataFromHistData{  

    @Override 
    public File downloadData(String formUrl) { 
    String tk = null; 
    String date= null; 
    String datemonth= null; 
    String platform= null; 
    String timeframe= null; 
    String fxpair= null; 

    WebClient webClient=new WebClient(); 

    try 
    { 
     HtmlPage page1=webClient.getPage(formUrl); 
     webClient.getOptions().setJavaScriptEnabled(false); 
     HtmlForm form=page1.getFormByName("file_down"); 
     tk=form.getInputByName("tk").getAttribute("value"); 
     date=form.getInputByName("date").getAttribute("value"); 
     datemonth=form.getInputByName("datemonth").getAttribute("value"); 
     platform=form.getInputByName("platform").getAttribute("value"); 
     timeframe=form.getInputByName("timeframe").getAttribute("value");; 
     fxpair=form.getInputByName("fxpair").getAttribute("value"); 
     System.out.println("fx"+fxpair); 
     webClient.close(); 

    } 
    catch (IOException e1) { 
     e1.printStackTrace(); 
    } 

    Client client = ClientBuilder.newBuilder().build(); 

    WebTarget target = client.target("http://www.histdata.com/get.php"); 

    MultivaluedMap<String, String> map = new MultivaluedStringMap(); 
    map.add("tk", tk); 
    map.add("date", date); 
    map.add("datemonth", datemonth); 
    map.add("platform", platform); 
    map.add("timeframe", timeframe); 
    map.add("fxpair", fxpair); 

    InputStream responseStream = target.request().header("Referer", "http://www.histdata.com/") 
      .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") 
      .header("Accept-Encoding", "tar, deflate") 
      .post(Entity.form(map), InputStream.class); 

     File file = new File("data.tar"); 

     OutputStream fos; 
     try 
     { 
      fos = new FileOutputStream(file); 
      byte[] buffer = new byte[1024]; 
      int len; 

      while ((len = responseStream.read(buffer)) != -1) 
      { 

       fos.write(buffer, 0, len); 
      } 
      fos.close(); 
     }  
     catch (FileNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace();  
     } 

     return file; 
} 

@Override 
public File unZipData(File zipFile) { 
    byte[] buffer = new byte[1024]; 
    final String OUTPUT_FOLDER = "/Users/venkateswara/Downloads/Sindhu/histdata"; 
    File returnFileName=null; 
    try{ 

     File folder = new File(OUTPUT_FOLDER); 
     if(!folder.exists()){ 
      folder.mkdir(); 
     } 

     ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); 
     ZipEntry ze = zis.getNextEntry(); 

     if(ze==null) 
     { 
      System.out.println("ZipFile is empty or corrupted"); 
      System.exit(0); 
     } 

     while(ze!=null){ 

      String fileName = ze.getName(); 
      File newFile = new File(OUTPUT_FOLDER + File.separator + fileName); 
      if(FilenameUtils.getExtension(newFile.getName()).contains("csv")) 
      { 
       returnFileName=newFile.getAbsoluteFile(); 
      } 

      //create all non exists folders 
      //else you will hit FileNotFoundException for compressed folder 
      new File(newFile.getParent()).mkdirs(); 

      FileOutputStream fos = new FileOutputStream(newFile);    

      int len; 
      while ((len = zis.read(buffer)) > 0) { 
      fos.write(buffer, 0, len); 
      } 

      fos.close(); 
      ze = zis.getNextEntry(); 
     } 
     zis.closeEntry(); 
     zis.close();  
    } 
    catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
    return returnFileName;  

} 

private InsertHistDao insertHistDao; 

@Autowired 
public void setInsertHistIntoDb(InsertHistDao insertHistDao) { 
    this.insertHistDao = insertHistDao; 
} 


@Override 
public void getDataFromCsv(File unZipFile,String sym) { 
    String[] csvLine=null; 
    try 
    { 
     CSVReader csvReader=new CSVReader(new FileReader(unZipFile)); 
     while((csvLine=csvReader.readNext())!=null) 
     { 
      System.out.println("data :"+Arrays.toString(csvLine)); 
      insertHistDao.insertHistIntoDb(csvLine,sym); 
      rowsInserted++; 
      System.out.println("Rows inserted:"+rowsInserted); 
     } 
     csvReader.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
    } 

這是我想要做單元測試的類。

回答

1

一般來說,你想聽聽這些videos。認真;如果你想做單元測試,那麼你應該退一步瞭解那個真的意味着

就你的情況而言,你的代碼不容易測試。 開始與:您的方法確實創建它正在使用的webClient對象使用。這意味着:您的測試代碼具有控制該對象的沒有。沒有控制,測試是不可能的!

相反,您可以使用依賴注入。這意味着:您創建一種方式,以便您的測試代碼可以將模擬的 webClient對象提供給您的待測試代碼。然後你可以簡單地描述一下:我預計這個電話會發生在我的模擬上;然後,應該返回該值。

但當然,這只是您的方法中的第一個對象。你的另一個問題是,你正在做的方式太多在這一個方法中的東西。相反,你必須分解成許多更小的東西。然後,您創建更多的類/對象,這些類/對象將參與不同的功能。你自己測試它們。

所以,長話短說:

  1. 你真的要退一步理解代碼,從單元測試的利潤......必須在一個非常不同的方式來寫!
  2. 爲了充分利用單元測試;您不僅需要了解「單元測試實踐」和框架,還需要了解「如何創建乾淨的代碼」和OO設計。
+0

@GostCat感謝您對我的代碼的反饋。這非常有幫助。但我沒有懷疑。 1.從您的反饋意見中的第一點開始,您指定我正在使用新的關鍵字及其不良做法。是否我們根本不應該使用'新'關鍵字來更好地控制代碼?因此,如果要爲ex創建一個新的File對象,我們是否應該使用依賴注入來創建它? 2.你能否藉助一些例子來說明你在第二點中指定的'分解成更小的東西'。 –

0

首先,我建議您嘗試編寫更容易的代碼塊的JUnit測試。只是簡單的返回值等。然後學習如何創建模擬以及爲什麼。 好的一點是,你可以編寫簡潔易懂的代碼。比您可以更輕鬆地編寫JUnit測試。

我總是遵循規則「AAA」(Arrange,Act,Assert)。排列 - 設置模擬,變量等。行動 - 在那裏你可以教你的嘲笑他們應該做什麼。斷言 - 這個模塊是用來確定你期望的值。

+0

感謝@Richard H.您的建議。 –