This is not necessarily a smell, and you can partially mock the Car as follows:
String carId = "..."; Car car = ...; CarServiceImpl car = mock(CarServiceImpl.class); when(car.findById(carId)).thenReturn(car); when(car.deleteById(carId)).thenCallRealMethod();
But , if you can let deleteById() execute the “real method”, then your test should have a repository, and in this case, let findById() be the “real call” simply and improve the quality of your test coverage in the absence of any additional costs. The fact that you have already tested findById() does not mean that you should not test it again, indirectly, as part of deleteById() .
I would suggest that you do one or both of the following:
- Unit test
Car , laughing at the repository and using the laughed expectations and checks to test all its methods Car Functional / Acceptance Test by providing it with a real repository and using real calls in the underlying repository to validate the actual results for each of its methods.
In a separate note, I suggest that the idea of injecting a repository into a domain object is an intentional use of the “active record” template, in which your entities know how CRUD themselves are. This can be considered a code smell; it violates the PSA and can be considered a poor separation of problems, because the domain subject knows about two things: its own state and how to persist.
source share