Skip to content

Commit

Permalink
remove thrown exception on findAllById not found
Browse files Browse the repository at this point in the history
spring-data CrudRepository doesn't throw exception if it doesn't find this entry, but it returns a smaller list.
https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html#findAllById(java.lang.Iterable)
  • Loading branch information
rd-moritz-richter authored and mkutz committed Dec 10, 2024
1 parent 6563f1c commit 634dac0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ public Iterable<T> findAllById(Iterable<ID> ids) {
List<T> found = new ArrayList<>();
ids.forEach(
id -> {
if (!data.containsKey(id)) {
throw new IllegalArgumentException("No entity with id %s".formatted(id));
if (data.containsKey(id)) {
found.add(data.get(id));
}
found.add(data.get(id));
});
return found;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.UUID.randomUUID;
import static java.util.stream.StreamSupport.stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.assertj.core.api.Assumptions.assumeThat;

import java.util.List;
Expand Down Expand Up @@ -81,8 +80,7 @@ void findAllById_not_found() {
var unknownId = randomUUID();
var ids = List.of(knownEntity.getUuid(), unknownId);

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> repository.findAllById(ids));
assertThat(repository.findAllById(ids)).containsExactly(knownEntity);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.UUID.randomUUID;
import static java.util.stream.StreamSupport.stream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.assertj.core.api.Assumptions.assumeThat;

import java.util.List;
Expand Down Expand Up @@ -72,8 +71,7 @@ void findAllById_not_found() {
var unknownId = randomUUID();
var ids = List.of(knownEntity.getUuid(), unknownId);

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> repository.findAllById(ids));
assertThat(repository.findAllById(ids)).containsExactly(knownEntity);
}

@Test
Expand Down

0 comments on commit 634dac0

Please sign in to comment.