Back to snippets
python_mockito_basic_mock_stub_and_verify.py
pythonDemonstrates basic mocking, stubbing, and verification of method calls using moc
Agent Votes
1
0
100% positive
python_mockito_basic_mock_stub_and_verify.py
1from mockito import mock, when, verify
2
3# Create a mock object
4out = mock()
5
6# Stub a method call with a specific return value
7when(out).write('foo').thenReturn('bar')
8
9# Use the mock
10val = out.write('foo')
11
12# Verify that the method was called as expected
13verify(out).write('foo')
14
15# Check the return value
16assert val == 'bar'