在另一种方法中模拟方法返回null

发布于 2025-02-02 22:15:41 字数 1570 浏览 3 评论 0原文

我有服务方法“ customerDetails”以获取有关客户的更多信息,并更改某些字段,这些字段在我对客户进行单元测试时使用其中的另一种方法“ getByid”,我嘲笑其他方法,但由于模拟返回null,该测试失败了。我尝试一些我发现的解决方案,例如检查依赖的顺序和使用@InjectMocks(我这样做),但它们对我不起作用,我不知道问题在哪里。

代码片段可以更好地了解我

customerservice

public class customerService {

    public Customer customerDetails(int id) {
        CustomerDto customer = getById(id) //here is the problem 
        // rest of the code
    }

    public CustomerDto getById(int id) {
        Optional<Customer> customer =
            this.customerRepository.findCustomerByIdAndIsDeletedFalse(id); //return null here

        if (!customer.isPresent()) {
            // code to throw Exception customer not found                
        }
        //code to retrieve customer 
    }
}

customerserviceTest

public class CustomerServiceTest {
    
    @Mock
    private CustomerRepository customerRepository;

    @InjectMocks
    private CustomerService customerService;

    @BeforeEach
    public void createMocks() {
        MockitoAnnotations.initMocks(this);
    }
    @Test
    public void testCustomerDetails() {
        CustomerDto actualResponse = DummyCutomer.createDto(); // the actualResponse is filled successfully 
    
        when(customerService.getById(actualResponse.getId()).thenReturn(actualResponse); // but here it send to getById null !! 
    
            //rest of code
        }
}                           

I have service method "customerDetails" to get more info about customer and change some field that using another method inside it "getById" when I do the unit test for customerDetails I mock the other method but the test faild because the mock return null . I try some solutions I found like checking the order of dependencies and using @InjectMocks (which I do)but they did not work for me and I do not know where the problem is.

code snippet to understand me better

customerService

public class customerService {

    public Customer customerDetails(int id) {
        CustomerDto customer = getById(id) //here is the problem 
        // rest of the code
    }

    public CustomerDto getById(int id) {
        Optional<Customer> customer =
            this.customerRepository.findCustomerByIdAndIsDeletedFalse(id); //return null here

        if (!customer.isPresent()) {
            // code to throw Exception customer not found                
        }
        //code to retrieve customer 
    }
}

customerServiceTest

public class CustomerServiceTest {
    
    @Mock
    private CustomerRepository customerRepository;

    @InjectMocks
    private CustomerService customerService;

    @BeforeEach
    public void createMocks() {
        MockitoAnnotations.initMocks(this);
    }
    @Test
    public void testCustomerDetails() {
        CustomerDto actualResponse = DummyCutomer.createDto(); // the actualResponse is filled successfully 
    
        when(customerService.getById(actualResponse.getId()).thenReturn(actualResponse); // but here it send to getById null !! 
    
            //rest of code
        }
}                           

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雨的味道风的声音 2025-02-09 22:15:41

您需要模拟customerrepository而不是getByid方法
例如:

when(customerRepository.findCustomerByIdAndIsDeletedFalse(actualResponse.getId()).thenReturn(Optional.of(WHAT_EVER_YOU_WANT));

在您的情况下,我认为您有拖车方案:

findcustomerbyidandisdeletedfalse =&gt;可选(客户实例)
findcustomerbyidandisdeletedfalse =&gt;可选。Empty()

You need to mock the customerRepository not the getById method
For example:

when(customerRepository.findCustomerByIdAndIsDeletedFalse(actualResponse.getId()).thenReturn(Optional.of(WHAT_EVER_YOU_WANT));

In your case I think you have tow scenarios:

findCustomerByIdAndIsDeletedFalse => Optional.of(a customer instance)
findCustomerByIdAndIsDeletedFalse => Optional.empty()

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