Home > AI > Backend > SpringBoot > mysql-connector-java >

Test mysql repository layer

Example

Tutorial.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "tutorials")
public class Tutorial {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
 
    @Column(name = "title")
    private String title;
 
    @Column(name = "description")
    private String description;
 
    @Column(name = "published")
    private boolean published;
 
    public Tutorial(String title, String description, boolean published) {
        this.title = title;
        this.description = description;
        this.published = published;
    }
 
}

TutorialRepository.java

1
2
3
4
public interface TutorialRepository extends JpaRepository<Tutorial, Long> {
    List<Tutorial> findByPublished(boolean published);
    List<Tutorial> findByTitleContaining(String title);
}

TutorialRepositoryTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
@RunWith(SpringRunner.class)
@SpringBootTest
public class TutorialRepositoryTest {
 
    @Autowired
    TutorialRepository repository;
 
 
    @Before
    public void setup() {
        repository.deleteAll();
    }
 
    @Test
    public void should_find_no_tutorials_if_repository_is_empty() {
 
        Iterable<Tutorial> tutorials = repository.findAll();
 
        assertThat(tutorials).isEmpty();
    }
 
    @Test
    public void should_store_a_tutorial() {
        Tutorial tutorial = repository.save(new Tutorial("Tut title", "Tut desc", true));
 
        assertThat(tutorial).hasFieldOrPropertyWithValue("title", "Tut title");
        assertThat(tutorial).hasFieldOrPropertyWithValue("description", "Tut desc");
        assertThat(tutorial).hasFieldOrPropertyWithValue("published", true);
    }
 
    @Test
    public void should_find_all_tutorials() {
        Tutorial tut1 = new Tutorial("Tut#1", "Desc#1", true);
        repository.save(tut1);
 
        Tutorial tut2 = new Tutorial("Tut#2", "Desc#2", false);
        repository.save(tut2);
 
        Tutorial tut3 = new Tutorial("Tut#3", "Desc#3", true);
        repository.save(tut3);
 
        Iterable<Tutorial> tutorials = repository.findAll();
 
        assertThat(tutorials).hasSize(3).contains(tut1, tut2, tut3);
    }
 
    @Test
    public void should_find_tutorial_by_id() {
        Tutorial tut1 = new Tutorial("Tut#1", "Desc#1", true);
        repository.save(tut1);
 
        Tutorial tut2 = new Tutorial("Tut#2", "Desc#2", false);
        repository.save(tut2);
 
        Tutorial foundTutorial = repository.findById(tut2.getId()).get();
 
        assertThat(foundTutorial).isEqualTo(tut2);
    }
 
    @Test
    public void should_find_published_tutorials() {
        Tutorial tut1 = new Tutorial("Tut#1", "Desc#1", true);
        repository.save(tut1);
 
        Tutorial tut2 = new Tutorial("Tut#2", "Desc#2", false);
        repository.save(tut2);
 
        Tutorial tut3 = new Tutorial("Tut#3", "Desc#3", true);
        repository.save(tut3);
 
        Iterable<Tutorial> tutorials = repository.findByPublished(true);
 
        assertThat(tutorials).hasSize(2).contains(tut1, tut3);
    }
 
    @Test
    public void should_find_tutorials_by_title_containing_string() {
        Tutorial tut1 = new Tutorial("Spring Boot Tut#1", "Desc#1", true);
        repository.save(tut1);
 
        Tutorial tut2 = new Tutorial("Java Tut#2", "Desc#2", false);
        repository.save(tut2);
 
        Tutorial tut3 = new Tutorial("Spring Data JPA Tut#3", "Desc#3", true);
        repository.save(tut3);
 
        Iterable<Tutorial> tutorials = repository.findByTitleContaining("ring");
 
        assertThat(tutorials).hasSize(2).contains(tut1, tut3);
    }
 
    @Test
    public void should_update_tutorial_by_id() {
        Tutorial tut1 = new Tutorial("Tut#1", "Desc#1", true);
        repository.save(tut1);
 
        Tutorial tut2 = new Tutorial("Tut#2", "Desc#2", false);
        repository.save(tut2);
 
        Tutorial updatedTut = new Tutorial("updated Tut#2", "updated Desc#2", true);
 
        Tutorial tut = repository.findById(tut2.getId()).get();
        tut.setTitle(updatedTut.getTitle());
        tut.setDescription(updatedTut.getDescription());
        tut.setPublished(updatedTut.isPublished());
        repository.save(tut);
 
        Tutorial checkTut = repository.findById(tut2.getId()).get();
 
        assertThat(checkTut.getId()).isEqualTo(tut2.getId());
        assertThat(checkTut.getTitle()).isEqualTo(updatedTut.getTitle());
        assertThat(checkTut.getDescription()).isEqualTo(updatedTut.getDescription());
        assertThat(checkTut.isPublished()).isEqualTo(updatedTut.isPublished());
    }
 
    @Test
    public void should_delete_tutorial_by_id() {
        Tutorial tut1 = new Tutorial("Tut#1", "Desc#1", true);
        repository.save(tut1);
 
        Tutorial tut2 = new Tutorial("Tut#2", "Desc#2", false);
        repository.save(tut2);
 
        Tutorial tut3 = new Tutorial("Tut#3", "Desc#3", true);
        repository.save(tut3);
 
        repository.deleteById(tut2.getId());
 
        Iterable<Tutorial> tutorials = repository.findAll();
 
        assertThat(tutorials).hasSize(2).contains(tut1, tut3);
    }
 
    @Test
    public void should_delete_all_tutorials() {
        repository.save(new Tutorial("Tut#1", "Desc#1", true));
        repository.save(new Tutorial("Tut#2", "Desc#2", false));
 
        repository.deleteAll();
 
        assertThat(repository.findAll()).isEmpty();
    }
}

Leave a Reply