Python, Mockito, Mocking Another Module's Imports
Question: Suppose I have module1.py

    
    
    from my_library import useful_function
    
    def do_something(x):
        return useful_function(x)
    

I am testing module2.py

    
    
    from module1 import do_something
    
    def do_something_else(x):
        return do_something(x+1)
    

useful_function does database calls, and I would therefore like to stub it out
to return a constant value.

How can I use mockito to stub out useful_function? I have tried importing
my_library, and using

    
    
    when( my_library ).useful_function('abc').thenReturn(...)
    

but that does not work. If I pause Eclipse and hover over the useful_function
line inside do_something(), it appears that useful_function() has not been
stubbed.


Answer: 
    import module1
    module1.useful_function = Mock(...)
    return do_something(x+1)
    

