問題
Java類中private方法通常只能被其所屬類的調用,其他類只能望而卻步,單元測試private方法也就一籌莫展。
嘗試解法:
- 在測試時,手動將private改為public,測試完后再將其改回。
- 將測試方法寫進private方法的所屬類中,這樣就能調用private方法了。
上述解法雖然可行,但這些解法或多或少地違背單元測試應遵守AIR原則。
單元測試在線上運行時,感覺像空氣(AIR)那樣透明,但在測試質量的保障上,卻是非常關鍵的。好的單元測試宏觀上來說,具有自動化、獨立性、可重復執行的特點。
- A:Automatic(自動化)
- I:Independent(獨立性)
- R:Repeatable(可重復)
解法
先創建一個測試目標類App作為示例,目標是測試App類中private方法callPrivateMethod():
public class App {public void doSomething() {callPrivateMethod();}private String callPrivateMethod() {return "Private method is called.";}}
一
我們可以用Java的反射特性來突破private的限制,從而對private方法進行單元測試:
單元測試代碼:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;import org.junit.Assert;
import org.junit.Test;public class AppTest {@Testpublic void test() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {App app = new App();Method privateMethod = app.getClass().getDeclaredMethod("callPrivateMethod");privateMethod.setAccessible(true);Assert.assertEquals("Private method is called.", privateMethod.invoke(app));}
}
二
引入第三方工具,如Spring測試框架。
引入依賴:
<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.3.5</version><scope>test</scope>
</dependency>
單元測試代碼:
import static org.junit.Assert.*;import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;public class AppTest {@Testpublic void test() {App app = new App();assertEquals("Private method is called.", //ReflectionTestUtils.invokeMethod(app, "callPrivateMethod", null));}}
參考
- Junit測試private方法
- 阿里巴巴Java開發手冊
- How do I test a private function or a class that has private methods, fields or inner classes?