java: how to mock Calendar.getInstance()?

You can mock it using PowerMock in combination with Mockito: On top of your class: @RunWith(PowerMockRunner.class) @PrepareForTest({ClassThatCallsTheCalendar.class}) The key to success is that you have to put the class where you use Calendar in PrepareForTest instead of Calendar itself because it is a system class. (I personally had to search a lot before I found … Read more

Difference between Dependency Injection and Mocking Framework (Ninject vs RhinoMocks or Moq)

Ninject is Dependency Injection for .NET. RhinoMocks and Moq are both mocking frameworks. Now both have nothing to do with each other. I really had trouble understanding both so here I go trying to explain. Dependency Injection: is an implementation (lets call it) of Inversion of Control. You don’t confuse the two. You are taking … Read more

Unit testing a python app that uses the requests library

It is in fact a little strange that the library has a blank page about end-user unit testing, while targeting user-friendliness and ease of use. There’s however an easy-to-use library by Dropbox, unsurprisingly called responses. Here is its intro post. It says they’ve failed to employ httpretty, while stating no reason of the fail, and … Read more

How to mock external dependencies in tests? [duplicate]

I would suggest you wrap the back-end function speed_control::increase in some trait: trait SpeedControlBackend { fn increase(); } struct RealSpeedControl; impl SpeedControlBackend for RealSpeedControl { fn increase() { speed_control::increase(); } } struct MockSpeedControl; impl SpeedControlBackend for MockSpeedControl { fn increase() { println!(“MockSpeedControl::increase called”); } } trait SpeedControl { fn other_function(&self) -> Result<(), ()>; } struct … Read more

Mock non-virtual method C++ (gmock)

It means you will have to templatize your production code. Using your example: CSumWind class definition: class CSumWnd : public CBaseWnd { private: bool MethodA() }; Mocked CSumWnd class definition: class MockCSumWnd : public CBaseWnd { private: MOCK_METHOD(MethodA, bool()); }; Production class which have to be tested with mocked class CSumWind. Now it becomes templated … Read more

Customizing unittest.mock.mock_open for iteration

The mock_open() object does indeed not implement iteration. If you are not using the file object as a context manager, you could use: m = unittest.mock.MagicMock(name=”open”, spec=open) m.return_value = iter(self.TEST_TEXT) with unittest.mock.patch(‘builtins.open’, m): Now open() returns an iterator, something that can be directly iterated over just like a file object can be, and it’ll also … Read more