在另一种方法中模拟方法返回null
我有服务方法“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要模拟
customerrepository
而不是getByid
方法例如:
在您的情况下,我认为您有拖车方案:
findcustomerbyidandisdeletedfalse =&gt;可选(客户实例)
findcustomerbyidandisdeletedfalse =&gt;可选。Empty()
You need to mock the
customerRepository
not thegetById
methodFor example:
In your case I think you have tow scenarios:
findCustomerByIdAndIsDeletedFalse => Optional.of(a customer instance)
findCustomerByIdAndIsDeletedFalse => Optional.empty()