There is a common problem in single testing, the private method in the side class cannot be called directly. During the processing, Xiaoyan changes the method permissions through reflection, conducts a single test, shares it, and directly uploads the code.
Simple test class
Generate a simple test class with only one private method.
Copy code The code is as follows:
Single test code
Copy code The code is as follows:
objMyClass = new MyClass();}/*** Use reflection to unit test the private and protect methods in the class * * @param $strMethodName string: reflection function name * @return ReflectionMethod obj: callback object*/protected static function getPrivateMethod ($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible(true);return $method;}/*** @brief: Test the call of private function*/public function testPrivateFunc(){$testCase = 'just a test string';//Reflect this class$testFunc = self::getPrivateMethod('privateFunc');$res = $testFunc->invokeArgs($this ->objMyClass, array($testCase));$this->assertEquals($testCase, $res);$this->expectOutputRegex('/success/i');// Capture no parameter exception test try { $testFunc->invokeArgs($this->transfer2Pscase, array());} catch (Exception $expected) {$this->assertNotNull($expected);return true;}$this->fail(self ::FAIL);}}
Run results
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)
Key code analysis
Encapsulates a reflection call of the method of the class under test; at the same time, if the access permission of the processing method before the return method is true, you can access the private function method.
Copy code The code is as follows:
/*** Use reflection to unit test the private and protect methods in the class * * @param $strMethodName string: reflection function name * @return ReflectionMethod obj: callback object*/protected static function getPrivateMethod($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible( true);return $method;}
Let me share with you how to use reflection to call another type of private method in java
We know that Java applications cannot access private methods of persistence classes, but Hibernate does not have this restriction. It can access methods at various levels, such as private, default, protected, and public. How does Hibernate implement this function? ?The answer is to use JAVA’s reflection mechanism, as follows:
<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>
The output result is: ---452345234----q31234132