mocking

Issue with stubbing/mocking the method with makes a DB call

时间秒杀一切 提交于 2021-02-10 06:22:27
问题 I am having issue with Mocking a JDBC call using the MockitoJUnitRunner. Somehow Mockito is not mocking the actual call even though I have below subbing line into the test class. when(readOnlyJdbcTemplate.query(anyString(), any(Object[].class), any(int[].class), any(FeatureCollectionResponseExtractor.class))).thenReturn(actual); Very similar mocking is working in another class for very similar type of method. The only difference between them is my other class does have 3 parameters instead of

Issue with stubbing/mocking the method with makes a DB call

∥☆過路亽.° 提交于 2021-02-10 06:22:08
问题 I am having issue with Mocking a JDBC call using the MockitoJUnitRunner. Somehow Mockito is not mocking the actual call even though I have below subbing line into the test class. when(readOnlyJdbcTemplate.query(anyString(), any(Object[].class), any(int[].class), any(FeatureCollectionResponseExtractor.class))).thenReturn(actual); Very similar mocking is working in another class for very similar type of method. The only difference between them is my other class does have 3 parameters instead of

in python how to mock only the file write but not the file read?

坚强是说给别人听的谎言 提交于 2021-02-08 10:06:30
问题 I am testing a function in which both def foo(): with open('input.dat', 'r') as f: .... with open('output.dat', 'w') as f: .... are used. But I only want to mock the write part. Is it possible to do that? Or some other strategy should be used for testing such a function? with patch('__builtin__.open') as m: foo() would fail to read the data. Thanks in advance. 回答1: I found the following solution: from mock import patch, mock_open with open('ref.dat') as f: ref_output = f.read() with open(

in python how to mock only the file write but not the file read?

泪湿孤枕 提交于 2021-02-08 10:05:10
问题 I am testing a function in which both def foo(): with open('input.dat', 'r') as f: .... with open('output.dat', 'w') as f: .... are used. But I only want to mock the write part. Is it possible to do that? Or some other strategy should be used for testing such a function? with patch('__builtin__.open') as m: foo() would fail to read the data. Thanks in advance. 回答1: I found the following solution: from mock import patch, mock_open with open('ref.dat') as f: ref_output = f.read() with open(

How can I mock object with nested attributes in Python?

冷暖自知 提交于 2021-02-08 03:57:15
问题 I want to mock the method is_room_member where invitee is a string and occupants is a list of string. If invitee = 'batman' and occupants = ['batman', 'superman'] the method is_room_member returns True . class Foo: @staticmethod def is_room_member(invitee, msg): return invitee in msg.frm.room.occupants msg is the object which needs to be mocked so that I can test this method. How can I test this method since it'll require this msg object which has nested attributes ? I want the test to be

How to mock forEach behavior with Mockito

佐手、 提交于 2021-02-07 14:36:41
问题 I want to make the following work, but I don't know how to mock forEach behavior properly. (The code is taken from a related question Testing Java enhanced for behavior with Mockito ) @Test public void aa() { Collection<String> fruits; Iterator<String> fruitIterator; fruitIterator = mock(Iterator.class); when(fruitIterator.hasNext()).thenReturn(true, true, true, false); when(fruitIterator.next()).thenReturn("Apple") .thenReturn("Banana").thenReturn("Pear"); fruits = mock(Collection.class);

How to mock forEach behavior with Mockito

随声附和 提交于 2021-02-07 14:31:01
问题 I want to make the following work, but I don't know how to mock forEach behavior properly. (The code is taken from a related question Testing Java enhanced for behavior with Mockito ) @Test public void aa() { Collection<String> fruits; Iterator<String> fruitIterator; fruitIterator = mock(Iterator.class); when(fruitIterator.hasNext()).thenReturn(true, true, true, false); when(fruitIterator.next()).thenReturn("Apple") .thenReturn("Banana").thenReturn("Pear"); fruits = mock(Collection.class);

How to use mock_open with json.load()?

℡╲_俬逩灬. 提交于 2021-02-07 13:48:05
问题 I'm trying to get a unit test working that validates a function that reads credentials from a JSON-encoded file. Since the credentials themselves aren't fixed, the unit test needs to provide some and then test that they are correctly retrieved. Here is the credentials function: def read_credentials(): basedir = os.path.dirname(__file__) with open(os.path.join(basedir, "authentication.json")) as f: data = json.load(f) return data["bot_name"], data["bot_password"] and here is the test: def test

Symfony2 conditional service declaration

送分小仙女□ 提交于 2021-02-07 13:37:51
问题 I'm currently trying to find a solid solution to change the dependencies of a Symfony2 service dynamically. In detail: I have a Services which uses a HTTP-Driver to communicate with an external API. class myAwesomeService { private $httpDriver; public function __construct( HTTDriverInterface $httpDriver ) { $this->httpDriver = $httpDriver; } public function transmitData($data) { $this->httpDriver->dispatch($data); } } While running the Behat tests on the CI, I'd like to use a httpMockDriver

TypeScript doesn't recognize my jest mock module

我只是一个虾纸丫 提交于 2021-02-07 13:36:55
问题 Suppose I have an index.ts that will import Database.ts and run some queries. To test this index.ts file, I want to mock the Database.ts because I don't want to connect to any real database. This is my index.ts : import { connect } from './Database' export async function runSomeQuery() { const connection = await connect() const result = await connection.query('SOME QUERY') return result } and here is the mock of database ( __mocks__/Database.ts ) const mockConnection = { query: jest.fn() }