У меня есть метод контроллера:
@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);
}
Когда я запускаю тест, я получаю исключение CustomerNotFoundException.
Что не так с этим миром или моими руками?) Должно работать, работает в другом проекте с таким же стеком. Э-э-э ...
Я обнаружил, что нет ни одной проблемы. Трассировка стека ниже:
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.