2015-06-16 50 views
0

我想在下面的类中为params方法编写测试用例。为私有方法编写测试用例的问题

问题而编写的JUnit测试案例:

的问题是,该方法是私有的,并且该方法里面调用超类的方法。我尝试使用了EasyMock在那里我将能够剿超类的构造函数

CustomListener customListenerMock=createMock(CustomListener.class); 
    expect(customListenerMock.getParam("CHECK_INTEGRITY")).andReturn(null); 
    expect(customListenerMock.getParam("WRITE_ANSWER")).andReturn(null);  

的文件说,我将能够剿这些方法时,他们被称为,可以给指定的输出即空在此呼叫案件。

现在我的问题是如何调用私有方法进行测试?我尝试通过使用反射API,但它不按需要工作。

代码:

Method InitialiseSecurityConfiguration = Listener .class.getDeclaredMethod(methodToTest, null); 
    InitialiseSecurityConfiguration.setAccessible(true); 
    InitialiseSecurityConfiguration.invoke(fileListenerObj); 

当我与反射API调用,这些方法调用就这样和超级根据需要CLAS方法不supressed。

注意:我正在使用遗留应用程序,并且不允许更改我的方法的可见性。

class Listener extends CustomListener{ 

/* 
     Some More methods 
*/ 

private boolean params() 
     { 
     String integrity = ""; 
     String sWAnswer = ""; 
     try 
     { 
      try 
      { 
      integrity = super.getParam("CHECK_INTEGRITY"); 
      sWAnswer = super.getParam("WRITE_ANSWER"); 


      **Some Business Logic** 

      super.info("Request Directory : " + sRequestPath); 
      super.info("Response Directory : " + sResponsePath); 
      super.info("Error Directory : " + sErrorPath); 
      } 
     } 
     catch (Exception ex) 
     { 
      bCheck = false; 
     } 
     return bCheck; 

       }//closing the method params 
}//Closing Listener class  

回答

3

我会写调用你感兴趣的私有方法的公共方法测试。

作为一般规则,这是很好的做法,编写单元测试,对一个类的公共API,因此即可以在不更改测试的情况下更改实现(即私有方法)。

公共API是如何使用类的,所以这就是应该测试的。

相关问题