Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Data
- @Builder(toBuilder = true)
- @AllArgsConstructor
- @NoArgsConstructor
- @Document
- public class User {
- private static final long serialVersionUID = 1L;
- @Id
- private String id;
- private String username;
- }
- @Repository
- public interface UserRepository extends MongoRepository<User, String> {
- @Query(delete = true, value = "{ '_id': ?0 }")
- long deleteUserById(String id);
- }
- @SpringBootTest
- public class UserTest {
- @Autowired
- private UserRepository userRepository;
- @Test
- void testUser() {
- userRepository.deleteAll();
- var savedUsers = userRepository.saveAll(List.of(
- User.builder().username("john").build(),
- User.builder().username("jane").build(),
- User.builder().username("kate").build(),
- User.builder().username("brian").build()
- ));
- assertThat(userRepository.findAll().size())
- .isEqualTo(4);
- var jane = savedUsers.stream()
- .filter(u -> u.getUsername().equals("jane"))
- .findFirst().orElseThrow();
- var numDeleted = userRepository.deleteUserById(jane.getId());
- assertThat(numDeleted)
- .isEqualTo(1L);
- assertThat(userRepository.findAll())
- .hasSize(3)
- .allMatch(u -> Set.of("john", "kate", "brian").contains(u.getUsername()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement