无法调用“”因为“”是无效的摩根托 /朱尼特
根据标题,我在测试服务方法时面临问题。我认为这应该是某个配置问题,或者更多的off of mockito用法。
有客户dataService的代码
@Service
public class CustomerDataService {
@Autowired
CustomerDataRepository customerDataRepository;
...
@Transactional
public void methodTotest(boolean consent, Long dialogId) {
CustomerData customer = customerDataRepository.findCustomerByDialogId(dialogId);
if (!consent) {
customerDataRepository.deleteById(customer.getCustomerId());
}else{
customer.setConsentGiven(true);
customerDataRepository.save(customer);
}
}
测试客户dataServicEtest中 @springboottest class customerdataserviceTest {
@Autowired
CustomerDataService customerDataService;
@MockBean
CustomerDataRepository customerDataRepository;
@Test
void removeCustomerDataWhenConsentIsNotGiven() {
CustomerData customerDataTest = customerData;
//when
customerDataService.giveConsent(false,22L);
//then
verify(customerDataRepository,times(1)).save(customerDataTest);
}
错误是
java.lang.NullPointerException: Cannot invoke "com.mypackage.CustomerData.getCustomerId()" because "customer" is null
这是myMethod的第二行 提前致谢
as per title I am facing an issue when testing a service method.I suppose it should be some config problem or much more some wrong usage of mockito.
There is the code of the CustomerDataService
@Service
public class CustomerDataService {
@Autowired
CustomerDataRepository customerDataRepository;
...
@Transactional
public void methodTotest(boolean consent, Long dialogId) {
CustomerData customer = customerDataRepository.findCustomerByDialogId(dialogId);
if (!consent) {
customerDataRepository.deleteById(customer.getCustomerId());
}else{
customer.setConsentGiven(true);
customerDataRepository.save(customer);
}
}
In the test CustomerDataServiceTest
@SpringBootTest
class CustomerDataServiceTest {
@Autowired
CustomerDataService customerDataService;
@MockBean
CustomerDataRepository customerDataRepository;
@Test
void removeCustomerDataWhenConsentIsNotGiven() {
CustomerData customerDataTest = customerData;
//when
customerDataService.giveConsent(false,22L);
//then
verify(customerDataRepository,times(1)).save(customerDataTest);
}
The error is
java.lang.NullPointerException: Cannot invoke "com.mypackage.CustomerData.getCustomerId()" because "customer" is null
this is the second line of the myMethod
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案很简单。您模拟了类
customerdatarepository
,但没有指示该模拟如果调用相应的方法该怎么做。然后,Mockito模拟然后默认为通过方法调用不做任何事情
,如果有返回值返回null
。由于您返回的客户data为null,因此在调用此对象时会得到NPE。在您的情况下,这是您通过调用
getCustomerId()
来获得的错误情况。要解决此问题,只需指示您的模拟,
您可以显然可以用
mockito.any()
mockito.anyint()
或 42 和新客户data()
带有您先前创建的对象。我想你明白了;)The solution is simple. You mocked a class
customerDataRepository
but did not instruct it the mock what to do if the corresponding method is called. Mockito mocks then default back ondoing nothing
by method call and if there is a return valuereturn null
.Since your returned customerData is null you get your NPE when calling on this object. In your case this is in the error case that you get by calling
getCustomerId()
.To solve this issue simply instruct your mock
you can obviously replace
Mockito.any()
withMockito.anyInt()
or42
andnew CustomerData()
with a object you previously created. I think you get the idea ;)假设您在将其发布到StackoverFlow之前刚刚校正了名称,并且您在测试中调用的方法:
giveConsent
实际上,与Method> Method> MethodTotest
的方法相同。在调用
customerdataservice.giveconstent(false,22L);
之前,您需要配置存储库以返回某些测试(不是null!或模拟)customerdata entity:note note :因为您是您的将
false
作为第一个变量,您将进入代码的此分支,在测试中,您期望调用
save()
方法,因此测试将失败。Assuming that you have just corrected method names before posting it to Stackoverflow, and method you are calling in the test:
giveConsent
is, actually, the same method asmethodTotest
of the CustomerDataService.Before calling
customerDataService.giveConsent(false,22L);
, you need to configure you repository to return some test (not null! or mocked) customerData entity:Note: since you are passing
false
as 1st variable, you will get to this branch of codeAnd in the test you are expecting
save()
method to be called, so the test will fail.