Bael 7394 (#15942)
* BAEL-7490 read write file in separate thread * Change the to try resources * Update the code to sync with article * BAEL-7394 @datatestjpa in junit * Added new module to parent
This commit is contained in:
parent
e9f9c8737c
commit
c1e287b9c9
@ -80,6 +80,7 @@
|
||||
<!-- no longer available or maintained. Ref: https://spring.io/projects/spring-data-gemfire#overview -->
|
||||
<module>spring-data-geode</module>
|
||||
<module>spring-data-jpa-annotations</module>
|
||||
<module>spring-data-jpa-annotations-2</module>
|
||||
<module>spring-data-jpa-crud</module>
|
||||
<module>spring-data-jpa-crud-2</module>
|
||||
<module>spring-data-jpa-enterprise</module>
|
||||
|
34
persistence-modules/spring-data-jpa-annotations-2/pom.xml
Normal file
34
persistence-modules/spring-data-jpa-annotations-2/pom.xml
Normal file
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-data-jpa-annotations-2</artifactId>
|
||||
<name>spring-data-jpa-annotations-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.persistence</groupId>
|
||||
<artifactId>jakarta.persistence-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,42 @@
|
||||
package com.baeldung.datajpatest;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "app_user")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.baeldung.datajpatest;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class UserApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserApplication.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.datajpatest;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
|
||||
User findByUsername(String username);
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.baeldung.datajpatest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.After;
|
||||
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.orm.jpa.DataJpaTest;
|
||||
|
||||
@DataJpaTest
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
private User testUser;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
// Initialize test data before each test method test
|
||||
testUser = new User();
|
||||
testUser.setUsername("testuser");
|
||||
testUser.setPassword("password");
|
||||
userRepository.save(testUser);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
// Release test data after each test method
|
||||
userRepository.delete(testUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenUser_whenSaved_thenCanBeFoundById() {
|
||||
// Verify that the user has been saved successfully
|
||||
User savedUser = userRepository.findById(testUser.getId())
|
||||
.orElse(null);
|
||||
|
||||
assertNotNull(savedUser);
|
||||
assertEquals(testUser.getUsername(), savedUser.getUsername());
|
||||
assertEquals(testUser.getPassword(), savedUser.getPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenUser_whenUpdated_thenCanBeFoundByIdWithUpdatedData() {
|
||||
testUser.setUsername("updatedUsername");
|
||||
userRepository.save(testUser);
|
||||
|
||||
User updatedUser = userRepository.findById(testUser.getId())
|
||||
.orElse(null);
|
||||
|
||||
assertNotNull(updatedUser);
|
||||
assertEquals("updatedUsername", updatedUser.getUsername());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenUser_whenFindByUsernameCalled_thenUserIsFound() {
|
||||
User foundUser = userRepository.findByUsername("testuser");
|
||||
|
||||
assertNotNull(foundUser);
|
||||
assertEquals("testuser", foundUser.getUsername());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user