2011-10-18 179 views
0

我已經對java代碼進行了一些集成測試,我想知道是否有任何方法來檢測每個集成測試的源和目標,例如如果我們有兩個組件A和B,組件A調用組件B,我們應該有一個集成測試來測試這兩個組件,當組件B調用組件A時,我們應該有另一個集成測試,我的問題是來自測試用例代碼,我們可以決定哪個組件是調用者,哪個組件是哪個組件是通過使用工具或特定庫自動調用的。單元和集成測試

public void GetPatientInfo() //testGetPatientInfo() 
{ 
    ArrayList<PatientInfo> patients = new ArrayList<PatientInfo>(); 
    String pid = "10"; 
    EMRService instance = new EMRService(); 
    instance.setPatients(patients); 
    PatientInfo p=new PatientInfo("10", "ali", 120, 200); 
    patients.add(p); 
    PatientInfo expResult = p; 
    PatientInfo result = instance.getPatientInfo(pid); 
    assertEquals(expResult, result); 
} 
+2

爲什麼你想知道哪些是呼叫?如果它們是相同的調用,那麼它應該沒有關係,如果它們是不同的調用,它們應該在不同的測試中:-)看起來對我來說很簡單。你希望從知道哪些信息中獲得什麼信息? – corsiKa

+0

@glowcoder感謝您的快速響應,想象我們有兩個類EMRService和PatientInfo,並且EMRService類實現了GetPatientInfo方法,在以下(集成)測試用例中,您可以很容易地注意到EMRService調用了PatientInfo,因此如果您決定刪除EMRService類從你的系統中,你不再需要這個集成測試(你也將它刪除),因爲EMRService類是調用者。反之PatientInfo是主叫方,那麼你必須之前更新與EMRService類集成測試中刪除它,因爲PatientInfo取決於它 –

+0

公共無效GetPatientInfo()// testGetPatientInfo(){ ArrayList的患者=新的ArrayList () ; String pid =「10」; EMRService實例= new EMRService(); instance.setPatients(患者); PatientInfo p = new PatientInfo(「10」,「ali」,120,200); patients.add(p); PatientInfo expResult = p; PatientInfo result = instance.getPatientInfo(pid); assertEquals(expResult,result); } –

回答

0

您可以使用instanceof運算符來確定類的類型。

假設你的類層次結構是這樣的:

interface Component { public void foo(Component bar); } 
class A implements Component {} 
class B implements Component {} 

你的函數看起來是這樣的:

public void foo(Component bar) 
{ 
    if(bar instanceof A) 
    // do one type of intergration tests 
    else if(bar is instanceof B) 
    // do other type of integration tests 
} 

另一種可能是使用AOP周圍-建議,或使用模擬考試。 如果您提供更多信息(如示例函數調用),我可能會提供更好的答案。

通常情況下,您會編寫2個不同的集成測試,其中一個假設函數是用A類調用的,另一個是用B類調用的。