Mocking static methods with Mockito

后端 未结 15 1175
Happy的楠姐
Happy的楠姐 2020-11-21 06:51

I\'ve written a factory to produce java.sql.Connection objects:

public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory         


        
15条回答
  •  温柔的废话
    2020-11-21 07:35

    Since that method is static, it already has everything you need to use it, so it defeats the purpose of mocking. Mocking the static methods is considered to be a bad practice.

    If you try to do that, it means there is something wrong with the way you want to perform testing.

    Of course you can use PowerMockito or any other framework capable of doing that, but try to rethink your approach.

    For example: try to mock/provide the objects, which that static method consumes instead.

提交回复
热议问题