EN
Spring Boot + Spring Data JPA + H2 - unit tests simple example - @DataJpaTest
4 points
Full and tested example of how to use Spring Boot + Spring Data JPA + H2 when writing unit tests.
Github repository with this example:
Download this example:
Project structure:
UserEntity.java
xxxxxxxxxx
1
package com.dirask.model;
2
3
import javax.persistence.*;
4
import java.util.Objects;
5
6
7
name = "users") (
8
public class UserEntity {
9
10
11
strategy = GenerationType.IDENTITY) (
12
private Long id;
13
14
private String name;
15
private int age;
16
17
public UserEntity() {
18
19
}
20
21
public UserEntity(String name, int age) {
22
this.name = name;
23
this.age = age;
24
}
25
26
public Long getId() {
27
return id;
28
}
29
30
public void setId(Long id) {
31
this.id = id;
32
}
33
34
public String getName() {
35
return name;
36
}
37
38
public void setName(String name) {
39
this.name = name;
40
}
41
42
public int getAge() {
43
return age;
44
}
45
46
public void setAge(int age) {
47
this.age = age;
48
}
49
50
51
public boolean equals(Object o) {
52
if (this == o) return true;
53
if (o == null || getClass() != o.getClass()) return false;
54
UserEntity user = (UserEntity) o;
55
return age == user.age && Objects.equals(id, user.id) && Objects.equals(name, user.name);
56
}
57
58
59
public int hashCode() {
60
return Objects.hash(id, name, age);
61
}
62
63
64
public String toString() {
65
return "User{" +
66
"id=" + id +
67
", name='" + name + '\'' +
68
", age=" + age +
69
'}';
70
}
71
}
UserRepository.java
xxxxxxxxxx
1
package com.dirask.repository;
2
3
import com.dirask.model.UserEntity;
4
import org.springframework.data.repository.CrudRepository;
5
import org.springframework.stereotype.Repository;
6
7
8
public interface UserRepository extends CrudRepository<UserEntity, Long> {
9
10
UserEntity findOneByName(String name);
11
}
Application.java
xxxxxxxxxx
1
package com.dirask;
2
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6
7
public class Application {
8
9
public static void main(String[] args) {
10
SpringApplication.run(Application.class, args);
11
}
12
}
application.properties
xxxxxxxxxx
1
spring.main.banner-mode=off
2
spring.datasource.platform=h2
3
spring.jpa.hibernate.ddl-auto=none
4
spring.h2.console.enabled=true
data-h2.sql
xxxxxxxxxx
1
INSERT INTO users(name, age) VALUES('Kate', 26);
schema-h2.sql
xxxxxxxxxx
1
CREATE TABLE `users` (
2
`id` INT(11) NOT NULL AUTO_INCREMENT,
3
`name` VARCHAR(255) NULL DEFAULT NULL,
4
`age` INT(11) NULL DEFAULT NULL,
5
PRIMARY KEY (`id`)
6
);
UserRepositoryTest.java
xxxxxxxxxx
1
package com.dirask.repository;
2
3
import com.dirask.model.UserEntity;
4
import org.junit.Test;
5
import org.junit.runner.RunWith;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
8
import org.springframework.test.context.junit4.SpringRunner;
9
10
import static org.assertj.core.api.Assertions.assertThat;
11
12
SpringRunner.class) (
13
14
public class UserRepositoryTest {
15
16
17
private UserRepository userRepository;
18
19
20
public void should_create_new_user() {
21
UserEntity user = new UserEntity("Tom", 25);
22
userRepository.save(user);
23
24
UserEntity userByName = userRepository.findOneByName("Tom");
25
assertThat(userByName).isNotNull();
26
27
// User{id=2, name='Tom', age=25}
28
System.out.println(userByName.toString());
29
}
30
31
32
public void should_find_all_users() {
33
Iterable<UserEntity> users = userRepository.findAll();
34
assertThat(users).hasSize(1);
35
}
36
37
38
public void should_delete_user() {
39
UserEntity userByName = userRepository.findOneByName("Kate");
40
assertThat(userByName).isNotNull();
41
42
userRepository.delete(userByName);
43
44
Iterable<UserEntity> users = userRepository.findAll();
45
assertThat(users).hasSize(0);
46
}
47
}