From 83aae330ac08d37af8cb0d37799d23e03c358a37 Mon Sep 17 00:00:00 2001 From: Amit Kumatr Date: Fri, 27 Jan 2023 00:54:56 +0530 Subject: [PATCH] added setup and teardown method --- .../UserRepositoryIntegrationTest.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/collections/vsstream/UserRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/collections/vsstream/UserRepositoryIntegrationTest.java index 9bf3774999..be25a82126 100644 --- a/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/collections/vsstream/UserRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-query-3/src/test/java/com/baeldung/spring/data/jpa/query/collections/vsstream/UserRepositoryIntegrationTest.java @@ -3,17 +3,20 @@ package com.baeldung.spring.data.jpa.query.collections.vsstream; import static org.assertj.core.api.Assertions.assertThat; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import java.util.stream.Stream; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; import com.baeldung.spring.data.jpa.collections.vsstream.User; import com.baeldung.spring.data.jpa.collections.vsstream.UserRepository; +import com.github.javafaker.Faker; @DataJpaTest class UserRepositoryIntegrationTest { @@ -21,6 +24,24 @@ class UserRepositoryIntegrationTest { @Autowired private UserRepository userRepository; + @BeforeEach + public void setup() { + Faker faker = new Faker(); + List people = IntStream.range(1, 100) + .parallel() + .mapToObj(i -> new User(faker.name() + .firstName(), faker.name() + .lastName(), faker.number() + .numberBetween(1, 100), i)) + .collect(Collectors.toList()); + userRepository.saveAll(people); + } + + @AfterEach + public void tearDown() { + userRepository.deleteAll(); + } + @Test public void whenAgeIs20_thenItShouldReturnAllUsersWhoseAgeIsGreaterThan20InAList() { List users = userRepository.findByAgeGreaterThan(20);