反射調(diào)用private方法實踐(php、java)
單測中有個普遍性的問題,被側(cè)類中的private方法無法直接調(diào)用。小拽在處理過程中通過反射改變方法權(quán)限,進行單測,分享一下,直接上代碼。
簡單被測試類
生成一個簡單的被測試類,只有個private方法。
<?php/** * 崔小渙單測的基本模板。 * * @author cuihuan * @date 2015/11/12 22:15:31 * @version $Revision:1.0$ **/class MyClass {/** * 私有方法 * * @param $params * @return bool */private function privateFunc($params){if(!isset($params)){return false;}echo "test success";return $params;}}
單測代碼
<?php/*************************************************************************** * * $Id: MyClassTest T,v 1.0 PsCaseTest cuihuan Exp$ * **************************************************************************//** * 崔小渙單測的基本模板。 * * @author cuihuan * @date 2015/11/12 22:09:31 * @version $Revision:1.0$ **/require_once ('./MyClass.php');class MyClassTest extends PHPUnit_Framework_TestCase {const CLASS_NAME = 'MyClass';const FAIL = 'fail';protected $objMyClass;/** * @brief setup: Sets up the fixture, for example, opens a network connection. * * 可以看做phpunit的構(gòu)造函數(shù) */public function setup() {date_default_timezone_set('PRC');$this->objMyClass = new MyClass();}/** * 利用反射,對類中的private 和 protect 方法進行單元測試 * * @param $strMethodName string :反射函數(shù)名 * @return ReflectionMethod obj :回調(diào)對象 */protected static function getPrivateMethod($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible(true);return $method;}/** * @brief :測試private函數(shù)的調(diào)用 */public function testPrivateFunc(){$testCase = 'just a test string';// 反射該類$testFunc = self::getPrivateMethod('privateFunc');$res = $testFunc->invokeArgs($this->objMyClass, array($testCase));$this->assertEquals($testCase, $res);$this->expectOutputRegex('/success/i');// 捕獲沒有參數(shù)異常測試try { $testFunc->invokeArgs($this->transfer2Pscase, array());} catch (Exception $expected) {$this->assertNotNull($expected);return true;}$this->fail(self::FAIL);}}
運行結(jié)果
cuihuan:test cuixiaohuan$ phpunit MyClassTest.php PHPUnit 4.8.6 by Sebastian Bergmann and contributors.Time: 103 ms, Memory: 11.75MbOK (1 test, 3 assertions)
關(guān)鍵代碼分析
封裝了一個,被測類方法的反射調(diào)用;同時,返回方法之前處理方法的接入權(quán)限為true,便可以訪問private的函數(shù)方法。
/** * 利用反射,對類中的private 和 protect 方法進行單元測試 * * @param $strMethodName string :反射函數(shù)名 * @return ReflectionMethod obj :回調(diào)對象 */protected static function getPrivateMethod($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible(true);return $method;}
下面給大家分享java中利用反射調(diào)用另一類的private方法
我們知道,Java應(yīng)用程序不能訪問持久化類的private方法,但Hibernate沒有這個限制,它能夠訪問各種級別的方法,如private, default, protected, public. Hibernate是如何實現(xiàn)該功能的呢?答案是利用JAVA的反射機制,如下:
<span style="font-size:14px;">import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class ReflectDemo { public static void main(String[] args) throws Exception { Method method = PackageClazz.class.getDeclaredMethod("privilegedMethod", new Class[]{String.class,String.class}); method.setAccessible(true); method.invoke(new PackageClazz(), "452345234","q31234132"); } } class PackageClazz { private void privilegedMethod(String invokerName,String adb) { System.out.println("---"+invokerName+"----"+adb); } }</span>
輸出結(jié)果為:---452345234----q31234132
相關(guān)文章
jquery+thinkphp實現(xiàn)跨域抓取數(shù)據(jù)的方法
這篇文章主要介紹了jquery+thinkphp實現(xiàn)跨域抓取數(shù)據(jù)的方法,結(jié)合實例形式分析了thinkPHP結(jié)合jQuery的ajax實現(xiàn)跨域抓取數(shù)據(jù)的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10Kindeditor編輯器添加圖片上傳水印功能(php代碼)
這篇文章主要為大家詳細介紹了Kindeditor編輯器加圖片上傳水印功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08PHP+fiddler抓包采集微信文章閱讀數(shù)點贊數(shù)的思路詳解
這篇文章主要介紹了PHP+fiddler抓包采集微信文章閱讀數(shù)點贊數(shù)的思路,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12Laravel5.4框架使用socialite實現(xiàn)github登錄的方法
這篇文章主要介紹了Laravel5.4框架使用socialite實現(xiàn)github登錄的方法,結(jié)合實例形式分析了Laravel相關(guān)下載、安裝、配置及github登陸、注冊、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03ioncube_loader_win_5.2.dll的錯誤解決方法
這篇文章主要介紹了ioncube_loader_win_5.2.dll的錯誤解決方法的相關(guān)資料,需要的朋友可以參考下2015-01-01