无法调用“”因为“”是无效的摩根托 /朱尼特

发布于 2025-01-26 19:05:34 字数 1237 浏览 3 评论 0原文

根据标题,我在测试服务方法时面临问题。我认为这应该是某个配置问题,或者更多的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情感失落者 2025-02-02 19:05:34

解决方案很简单。您模拟了类customerdatarepository,但没有指示该模拟如果调用相应的方法该怎么做。然后,Mockito模拟然后默认为通过方法调用不做任何事情,如果有返回值返回null
由于您返回的客户data为null,因此在调用此对象时会得到NPE。在您的情况下,这是您通过调用getCustomerId()来获得的错误情况。

要解决此问题,只需指示您的模拟,

@Test
void removeCustomerDataWhenConsentIsNotGiven() {
   CustomerData customerDataTest = customerData;
   //when
   Mockito.when(customerDataRepository.findCustomerByDialogId(Mockito.any())).thenReturn(new CustomerData()); // <-- Add this line
   customerDataService.giveConsent(false,22L);
   //then 
   verify(customerDataRepository,times(1)).save(customerDataTest);
}

您可以显然可以用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 on doing nothing by method call and if there is a return value return 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

@Test
void removeCustomerDataWhenConsentIsNotGiven() {
   CustomerData customerDataTest = customerData;
   //when
   Mockito.when(customerDataRepository.findCustomerByDialogId(Mockito.any())).thenReturn(new CustomerData()); // <-- Add this line
   customerDataService.giveConsent(false,22L);
   //then 
   verify(customerDataRepository,times(1)).save(customerDataTest);
}

you can obviously replace Mockito.any() with Mockito.anyInt() or 42 and new CustomerData() with a object you previously created. I think you get the idea ;)

吻安 2025-02-02 19:05:34

假设您在将其发布到StackoverFlow之前刚刚校正了名称,并且您在测试中调用的方法:giveConsent实际上,与Method> Method> MethodTotest的方法相同。

在调用customerdataservice.giveconstent(false,22L);之前,您需要配置存储库以返回某些测试(不是null!或模拟)customerdata entity:

when(customerDataRepository.findCustomerByDialogId(22L)).thenReturn(customerDataTest);
customerDataService.giveConsent(false,22L);

note note :因为您是您的将false作为第一个变量,您将进入代码的此分支

    if (!consent) {  
      customerDataRepository.deleteById(customer.getCustomerId());
    }

,在测试中,您期望调用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 as methodTotest 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:

when(customerDataRepository.findCustomerByDialogId(22L)).thenReturn(customerDataTest);
customerDataService.giveConsent(false,22L);

Note: since you are passing false as 1st variable, you will get to this branch of code

    if (!consent) {  
      customerDataRepository.deleteById(customer.getCustomerId());
    }

And in the test you are expecting save() method to be called, so the test will fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文