問題描述
我的代碼中有一個靜態(tài)方法,我想以某種方式模擬.
I have a static method in my code that I would like somehow to mock.
我正在使用 jmock.
I am using jmock.
我想我可以做到這一點的一種方法是在靜態(tài)方法周圍設置包裝類",然后模擬這個,但我希望有更好的解決方案.
One way I suppose I could do this is to have "wrapper class" around the static method and mock this but I was hoping for a better solution.
我想錯了嗎?
反饋:
我將擁有一個接口和類,其中包含一個調(diào)用靜態(tài)方法的方法.它允許我通過模擬對這個包裝類的調(diào)用來模擬邏輯.(說起來我都覺得很臟:))
I was going to have a interface and class that had a method that just called the static method. It would allow me to mock the logic by just mocking the call to this wrapper class. (I feel dirty even talking about it :) )
推薦答案
我們不支持在 jMock 中模擬靜態(tài)方法,因為它不適合我們的設計方法.對于可能影響系統(tǒng)狀態(tài)的重要功能,我們不喜歡使用靜態(tài)方法.我們傾向于使用它們只是為了支持 OO 代碼并使其更具可讀性.這就是為什么我們將模擬靜態(tài)方法視為存在問題的暗示.一個例外是它位于第三方庫中,但無論如何我們可能會將其包裝在更面向?qū)ο蟮臇|西中.
We don't support mocking static methods in jMock because it doesn't fit our design approach. We prefer not to use static methods for significant features that can affect the state of the system. We tend to use them just to support the OO code and make it more readable. That's why we view mocking a static methods as a hint that there's a problem. One exception is where it's in a third-party library, but we would probably wrap that in something more object-oriented anyway.
這篇關于jmock 模擬靜態(tài)方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!