added code of collections-vs-stream

This commit is contained in:
Amit Kumatr 2023-01-27 00:32:13 +05:30
parent 69008b0f0d
commit e04bb7bc91
4 changed files with 121 additions and 0 deletions

View File

@ -22,6 +22,11 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>0.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -0,0 +1,61 @@
package com.baeldung.spring.data.jpa.collections.vsstream;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "_user")
public class User {
private String firstName;
private String lastName;
private int age;
@Id
private int id;
public User() {
}
public User(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public User(String firstName, String lastName, int age, int id) {
this(firstName, lastName, age);
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.spring.data.jpa.collections.vsstream;
import java.util.List;
import java.util.stream.Stream;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, String> {
Stream<User> findAllByAgeGreaterThan(int age);
List<User> findByAgeGreaterThan(int age);
}

View File

@ -0,0 +1,41 @@
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.Stream;
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;
@DataJpaTest
class UserRepositoryIntegrationTest {
@Autowired
private UserRepository userRepository;
@Test
public void whenAgeIs20_thenItShouldReturnAllUsersWhoseAgeIsGreaterThan20InAList() {
List<User> users = userRepository.findByAgeGreaterThan(20);
assertThat(users).isNotEmpty();
assertThat(users.stream()
.map(User::getAge)
.allMatch(age -> age > 20)).isTrue();
}
@Test
@Transactional
public void whenAgeIs20_thenItShouldReturnAllUsersWhoseAgeIsGreaterThan20InAStream() {
Stream<User> users = userRepository.findAllByAgeGreaterThan(20);
assertThat(users).isNotNull();
assertThat(users.map(User::getAge)
.allMatch(age -> age > 20)).isTrue();
}
}