Java Mockito的Wierd错误。when(repository.findById(any(Long.class)).thenReturn(可选的.of(用户))返回null
我有一个控制器的方法:Java Mockito的Wierd错误。when(repository.findById(any(Long.class)).thenReturn(可选的.of(用户))返回null,java,spring,unit-testing,mockito,Java,Spring,Unit Testing,Mockito,我有一个控制器的方法: @GetMapping("/{id}") public Customer getCustomer(@PathVariable Long id) { return customerRepository.findById(id).orElseThrow(CustomerNotFoundException::new); } 我有一个测试类和一个方法: @ExtendWith(MockitoExtension.class) @SpringBoot
@GetMapping("/{id}")
public Customer getCustomer(@PathVariable Long id) {
return customerRepository.findById(id).orElseThrow(CustomerNotFoundException::new);
}
我有一个测试类和一个方法:
@ExtendWith(MockitoExtension.class)
@SpringBootTest
class CustomerControllerTest {
@InjectMocks
@Autowired
private CustomerController controller;
@Mock
private CustomerRepository customerRepository;
@Mock
private UserRepository userRepository;
private Customer customer;
private List<Customer> customerList;
....
@Test
void getCustomer() {
Customer customer1 = CustomerProviderUtils.generateEntity();
when(customerRepository.findById(any(Long.class))).thenReturn(Optional.of(customer1));
Customer responseCustomer = controller.getCustomer(1L);
assertNotNull(responseCustomer);
}
@ExtendWith(MockitoExtension.class)
@春靴测试
类CustomerController测试{
@注射模拟
@自动连线
私人客户控制器;
@嘲弄
私人客户存储客户存储;
@嘲弄
私有用户存储库用户存储库;
私人客户;
私人名单客户名单;
....
@试验
void getCustomer(){
Customer customer1=CustomerProviderUtils.generateEntity();
when(customerRepository.findById(any(Long.class)),然后return(可选的)(customer1));
客户响应客户=控制器.getCustomer(1L);
assertNotNull(responseCustomer);
}
当我运行测试时,我得到一个CustomerNotFoundException
这个世界或我的手有什么问题?)它应该是有效的,它在其他项目中也同样有效
我发现没有一个问题。stacktrace如下所示:
com.jundevinc.internationalpizza.exception.CustomerNotFoundException
at java.util.Optional.orElseThrow(Optional.java:290)
at com.jundevinc.internationalpizza.api.controller.CustomerController.getCustomer(CustomerController.java:42)
at com.jundevinc.internationalpizza.api.controller.CustomerController$$FastClassBySpringCGLIB$$6f790393.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685)
at com.jundevinc.internationalpizza.api.controller.CustomerController$$EnhancerBySpringCGLIB$$48cee886.getCustomer(<generated>)
at com.jundevinc.internationalpizza.api.controller.CustomerControllerTest.getCustomer(CustomerControllerTest.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
.......
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
1. -> at com.jundevinc.internationalpizza.api.controller.CustomerControllerTest.getCustomer(CustomerControllerTest.java:66)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.
com.jundevinc.internationalp批萨.exception.CustomerNotFoundException
在java.util.Optional.orelsetrow(Optional.java:290)
在com.jundevinc.internationalp批萨.api.controller.CustomerController.getCustomer上(CustomerController.java:42)
在com.jundevinc.internationalp批萨.api.controller.CustomerController$$FastClassBySpringCGLIB$$6f790393.invoke()上
位于org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
位于org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685)
在com.jundevinc.internationalp批萨.api.controller.CustomerController$$EnhancerBySpringCGLIB$$48cee886.getCustomer()上
在com.jundevinc.internationalp批萨.api.controller.CustomerControllerTest.getCustomer(CustomerControllerTest.java:67)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
.......
检测到不必要的存根。
干净且可维护的测试代码不需要任何不必要的代码。
以下存根是不必要的(单击导航到相关代码行):
1.->com.jundevinc.internationalp批萨.api.controller.CustomerControllerTest.getCustomer(CustomerControllerTest.java:66)
请删除不必要的存根或使用“宽松”严格。更多信息:javadoc用于不必要的存根异常类。
您将用于单元测试的Mockito注释(InjectMock、Mock)与Spring注释(Autowired、SpringBootTest)混合在一起,用于集成测试。控制器是由Spring创建的,不会在其中注入模拟存储库。选择是否需要单元测试,不支持任何Spring,以及您或Mockito注释在何处创建所有内容,或者选择集成测试(使用Autowired和MockBean),这里是Spring创建一切的地方。JB Nizet,非常感谢!)我删除了Autowired,现在一切都正常了)你也应该删除SpringBootTestJB Nizet,哦,我的天啊,现在不是加载所有上下文节省了很多时间!非常感谢!)