2011-10-18 55 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类调用的。