@SpringBootTest
class DetailServiceV1Test {
@Mock
private DetailRepository detailRepository;
@Autowired
private final DetailService detailService = new DetailServiceV1(detailRepository);
@Test
void getUsedDetails() {
Detail detail = new Detail(101L, "children", "code", new HashSet<>(), 100L, 0, 100L, new
ArrayList<>());
Detail parent = new Detail(100L, "parent", "code", new HashSet<>(), null, 1, 100L, new
ArrayList<>());
List<Detail> detailList = List.of(parent, detail);
when(detailRepository.findAll()).thenReturn(detailList);
List<Detail> usedDetails = detailService.getUsedDetails(detail);
System.out.println(usedDetails.toString());
Detail test = usedDetails.get(0);
assertEquals(test, parent);
}
}
Подскажите пожалуйста, что не так прикручиваю. Лист userDetails пустой, хотя по логике в нем должен быть объект parent.
public class DetailServiceV1 implements DetailService {
private final DetailRepository detailRepository;
public DetailServiceV1(DetailRepository detailRepository) {
this.detailRepository = detailRepository;
}
@Override
public List<Detail> getUsedDetails(Detail detail) {
List<Detail> details = detailRepository.findAll();
List<Detail> usedDetails = new ArrayList<>();
for (Detail detailInList : details) {
if (Objects.equals(detailInList.getParentId(), detail.getId())) {
usedDetails.add(detailInList);
log.info("find used detail");
}
}
return usedDetails;
}
DetailRepository наследует JpaRepository<Detail, Long>
equals()
иhashCode()
у классаDetail
переопределили?