在Java对象构造函数中模拟依赖项

huangapple go评论69阅读模式
英文:

Mocking dependencies in java object constructor

问题

以下是翻译好的内容:

我有一个由其他对象/依赖项组成的类,如下所示:

public class One{

    private RedisPool redisPool;

    private static final WeakHashMap<String, Dedup<String>> CACHE = new WeakHashMap<>();

    private static final ObjectMapper mapper = new ObjectMapper();

    private BigQueryManager bigQueryManager;

    private RedisClientManager redisClientManager;

    private PubSubIntegration pubSub;

    public One(RedisPool redisPool, Configuration config) {

        this.redisPool = redisPool;

        bigQueryManager = new BigQueryManager(config);

        redisClientManager = new RedisClientManager(redisPool, config);

        pubSub = new PubSubIntegration(config);

    }

.....
...
其他方法

}

我尝试了:

public class OneTest{

   @Mock
   RedisPool redisPool;

   @Mock
   Configuration config;

   @Before
   public void setUp(){
       MockitoAnnotations.initMocks(this);
       One one = new One(redisPool, config);
   }
}

但我不确定在构造函数内部构造的对象是否也会被模拟,因为我在类内的其他方法中使用了这些对象。

我该如何对构造函数内部创建的对象进行模拟?

英文:

I have a class that's composed of other objects/dependencies as follows:

public class One{
  
    private RedisPool redisPool;

	private static final WeakHashMap&lt;String, Dedup&lt;String&gt;&gt; CACHE = new WeakHashMap&lt;&gt;();

	private static final ObjectMapper mapper = new ObjectMapper();

	private BigQueryManager bigQueryManager;

	private RedisClientManager redisClientManager;

	private PubSubIntegration pubSub;

	public One(RedisPool redisPool, Configuration config) {

		this.redisPool = redisPool;

		bigQueryManager = new BigQueryManager(config);

		redisClientManager = new RedisClientManager(redisPool, config);

		pubSub = new PubSubIntegration(config);

	}

....
...
other methods    

}

I tried:

public class OneTest{
    
   @Mock
   RedisPool redisPool;

   @Mock
   Configuration config;

   @Before
   public void setUp(){
       MockitoAnnotations.initMocks(this);
       One one = new One(redisPool, config);
   }
}

But I'm not sure whether the objects constructed inside the constructor also get mocked, as I have used those objects in other methods inside the class.

How can I mock the objects constructed inside the constructor?

答案1

得分: 1

我不认为你可以仅使用Mockito来实现这一点,但如果你也使用PowerMockito的话,是可以的。

这篇文章提供了一个与你想要实现的相似的示例。

注意,PowerMockito会影响你正在收集的任何测试覆盖率统计数据。

编辑

如果这篇文章消失了,它的要点是你需要稍微不同地注释你的测试类:

@RunWith(PowerMockRunner.class)
@PrepareForTest(One.class)
public class OneTest {

@PrepareForTest注解指的是你需要改变行为的类 - 在你的情况下是One类。

然后,告诉PowerMockito在创建新实例时返回一个你可以控制的对象。在你的情况下:

PowerMockito.whenNew(BigQueryManager.class)
        .withAnyArguments().thenReturn(mockBigQueryManager);
英文:

I don't think you can achieve this using Mockito alone, but you can if you also use PowerMockito.

This article gives an example that is quite similar to what you want to achieve.

Note that PowerMockitio will mess up any test coverage statistics that you are collecting.

EDIT

If the article disappears, the gist of it is that you need to annotate your Test class slightly differently

@RunWith(PowerMockRunner.class)
@PrepareForTest(One.class)
public class OneTest {

The @PrepareForTest annotation refers to the Class that you need to alter the bahaviour of - in your case the One class.

You then tell PowerMoncito to return an object you can control when a new instance is created. In your case

PowerMockito.whenNew(BigQueryManager.class)
			.withAnyArguments().thenReturn(mockBigQueryManager);

huangapple
  • 本文由 发表于 2020年8月31日 18:05:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63668747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定