Author Login
Post Reply
Does the static method have side effects (e.g. change mutable static
state)? If so, it's probably best to turn that into an object and
give it an interface. You can then mock the interface.
If it's a pure function with no side effects, then there's no problem
with giving the object under test known inputs and testing how it uses
the known outputs from the pure function.
If you want to ignore the complexity of the function when testing,
then I think you should apply the Strategy pattern. Introduce an
interface between the object under test and the strategy, use a
strategy implementation that calls (or replaces) the static function
in production code, and use stub implementation in tests.
--Nat
2009/12/1 Siddhi B <borkar.siddhi@(protected)>:
>
> Thanks for your quick reply Steve.
> The above code snippet was just a simple example. The code that I am working
> on contains complex logic in the static function.
>
>
>
> Steve Freeman-2 wrote:
>>
>> We don't support mocking of static methods since there is almost
>> always a better way. That might mean testing it directly in an
>> integration test or writing a thin layer above the static method and
>> testing against /that/.
>>
>> If your method is as simple as getName() it might be better to just
>> call the method.
>> S.
>>
>> On 1 Dec 2009, at 03:37, Siddhi B <borkar.siddhi@(protected):
>>> Hi,
>>>
>>> I need a way of mocking a static class using JMock. Consider the
>>> following
>>> code snippet.
>>>
>>> public class A {
>>> void functionX() {
>>> String name = ClassB.getName(); // Static function
>>> }
>>> }
>>>
>>> public class ClassB {
>>> static String getName() {
>>> return 'ABC'
>>> }
>>> }
>>>
>>> I need a way of mocking the static function 'getName'.Please note
>>> that I do
>>> not have rights to modify the legacy code.
>>>
>>> Any help would be highly appreciated.
>>>
>>> Thanks,
>>> Siddhi
>>> --
>>> View this message in context:
>>> http://old.nabble.com/Mocking-Static-Methods-using-JMock-tp26585954p26585954.html
>>> Sent from the jMock - User mailing list archive at Nabble.com.
>>>
>>>
>>> ---------------------------------------------------------------------
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>> http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/Mocking-Static-Methods-using-JMock-tp26585954p26588111.html
> Sent from the jMock - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>
--
http://www.natpryce.com
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email