Java 单元与集成测试

Java 单元与集成测试,java,Java,我已经有一些java代码的集成测试,我想知道是否有任何方法可以检测每个集成测试的源和目标,例如,如果我们有两个组件A和B,当组件A调用组件B时,我们应该有一个集成测试来一起测试这两个组件,当组件B调用组件A时,我们应该进行另一个集成测试,我的问题来自测试用例代码,我们可以通过使用工具或特定库自动确定哪个组件是调用者,哪个是被调用者吗 public void GetPatientInfo() //testGetPatientInfo() { ArrayList<PatientInf

我已经有一些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); 
}
public void GetPatientInfo()//testGetPatientInfo()
{
ArrayList患者=新ArrayList();
字符串pid=“10”;
EMRService实例=新的EMRService();
例:患者;
PatientInfo p=新的PatientInfo(“10”,“阿里”,120200);
加(p);
PatientInfo expResult=p;
PatientInfo结果=instance.getPatientInfo(pid);
assertEquals(expResult,result);
}

您可以使用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
}
您的函数可以如下所示:

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和around建议,或者使用mock。 如果您提供更多信息(如示例函数调用),我可能能够提供更好的答案


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

为什么您想知道调用的是哪个?如果它们是同一个调用,则不重要,如果它们是不同的调用,则应在不同的测试中:-)对我来说似乎非常简单。您希望从知道哪个调用中获得什么信息?@glowcoder感谢您的快速响应,假设我们有两个类EMRService和PatientInfo,EMRService类实现GetPatientInfo方法,在下面的(集成)测试用例中,您可以很容易地注意到EMRService调用PatientInfo,因此,如果您决定从系统中删除EMRService类,您将不再需要此集成测试(您也将删除它),因为EMRService类是调用方。否则,如果PatientInfo是调用方,则必须在删除它之前使用EMRService类更新集成测试,因为PatientInfo依赖于它public void GetPatientInfo()//testGetPatientInfo(){ArrayList patients=new ArrayList();String pid=“10”;EMRService实例=new EMRService());instance.setPatients(patients);PatientInfo p=new PatientInfo(“10”,“ali”,120200);patients.add(p);PatientInfo expResult=p;PatientInfo result=instance.getPatientInfo(pid);assertEquals(expResult,result);}