单元和集成测试

时间:2011-10-18 18:28:04

标签: java

我已经对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); 
}

1 个答案:

答案 0 :(得分: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和周围建议,或使用模拟。 如果您提供更多信息(例如函数调用),我可能会提供更好的答案。

通常你会写两个不同的集成测试,一个假设用A类调用函数,另一个用B类调用。