模拟类特定的方法(其余应该是真实的)
我有一个要测试的课程,还可以嘲笑一种特定的方法。 我创建了一个间谍,如下所示:
recertialssservice partialMock = spy(recertialssservice.class);
并在我的测试中调用我要测试的方法: partialmock.process(客户端,ID,URL,NULL,userDetails);
在真实方法中:
@Autowired private Encryptionservice engryptionservice;
我的休息在NullPoInterException上失败了,因为该类的自动病毒之一是null
。 在此行中:
Credential credentials = new Credential(id, encryptionService, tokenResponseParams.getAccessToken(), tokenResponseParams.getRefreshToken(), tokenResponseParams.getExpiresIn());
Encryptionservice
是null,并且创建凭据
类的操作之一是对此失败的。
任何知道为什么我的间谍不将gentionservice
作为bean的真实实例以及如何解决?
I have a class that I want to test, and also mock a specific method.
I've created a spy as follows:
CredentialsService partialMock = spy(CredentialsService.class);
And call in my test to a method I want to test:partialMock.process(client, id, url, null, userDetails);
In real method:
@Autowired private EncryptionService encryptionService;
My rest fails on NullPointerException, because one of the autowires of the class is NULL
.
In this line:
Credential credentials = new Credential(id, encryptionService, tokenResponseParams.getAccessToken(), tokenResponseParams.getRefreshToken(), tokenResponseParams.getExpiresIn());
encryptionService
is null and one of the operation in creating the Credential
class fail on this.
Any idea why my spy does not keep the real instance of encryptionService
as a bean, and how I can fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仅在
spy()
方法中仅使用类参数创建间谍,因此Mockito不知道应该注入创建的间谍对象中的任何对象。要注入丢失的依赖关系,您可以选择三种方法之一(一种无需修改实际代码)。为了简化,在示例中,我剥离了对象,因为没有提供完整的代码。
让我们首先假设现场注射(这更多地介绍了为什么它会觉醒要使用现场注入):
注释
对于下面的两种方法,我们都需要将依赖项注入构造函数,因此
recertentialsservice
看起来像这样:模拟设置
(在对象上读取更多信息)
spy
我通常使用上述方法中的第一个或最后一个方法(但第一个使用构造函数注入依赖项的方法 -它也起作用)。
You are creating a spy using only a class parameter in the
spy()
method, so Mockito does not know about any object that should be injected into the created spy object.To inject the missing dependency you can pick one of three approaches (one without modification of the actual code). For the sake of simplification in the example I've stripped the objects as complete code was not provided.
Let's assume field injection first (here's more on why it's discouraged to use field injection):
Annotations
For both approaches below we will need the dependencies to be injected in a constructor, so
CredentialsService
looks like this:Mock settings
(read more in the Mockito documentation)
Spy on an object
I'm usually using the first or last approach from the ones above (but the first one with dependencies injected by constructor - it works as well).