forked from scottfrederick/spring-music
-
Notifications
You must be signed in to change notification settings - Fork 670
Expand file tree
/
Copy pathAlbumControllerTest.java
More file actions
74 lines (59 loc) · 2.13 KB
/
Copy pathAlbumControllerTest.java
File metadata and controls
74 lines (59 loc) · 2.13 KB
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
package org.cloudfoundry.samples.music.web;
import org.cloudfoundry.samples.music.domain.Album;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.repository.CrudRepository;
import java.util.Arrays;
import java.util.Optional;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class AlbumControllerTest {
@Mock
private CrudRepository<Album, String> repository;
@InjectMocks
private AlbumController albumController;
@Test
public void testAlbums() {
Album album1 = new Album("Title1", "Artist1", "2020", "Pop");
Album album2 = new Album("Title2", "Artist2", "2021", "Rock");
when(repository.findAll()).thenReturn(Arrays.asList(album1, album2));
Iterable<Album> albums = albumController.albums();
assertNotNull(albums);
int count = 0;
for (Album a : albums) count++;
assertEquals(2, count);
}
@Test
public void testAdd() {
Album album = new Album("Title", "Artist", "2020", "Pop");
when(repository.save(album)).thenReturn(album);
Album added = albumController.add(album);
assertNotNull(added);
assertEquals("Title", added.getTitle());
}
@Test
public void testUpdate() {
Album album = new Album("Title", "Artist", "2020", "Pop");
when(repository.save(album)).thenReturn(album);
Album updated = albumController.update(album);
assertNotNull(updated);
assertEquals("Title", updated.getTitle());
}
@Test
public void testGetById() {
Album album = new Album("Title", "Artist", "2020", "Pop");
when(repository.findById("1")).thenReturn(Optional.of(album));
Album fetched = albumController.getById("1");
assertNotNull(fetched);
assertEquals("Title", fetched.getTitle());
}
@Test
public void testDeleteById() {
albumController.deleteById("1");
verify(repository, times(1)).deleteById("1");
}
}