File tree 5 files changed +111
-2
lines changed
test/java/com/hhimanshu/business
5 files changed +111
-2
lines changed Original file line number Diff line number Diff line change 37
37
<groupId >org.springframework.boot</groupId >
38
38
<artifactId >spring-boot-starter-web</artifactId >
39
39
</dependency >
40
-
40
+ <dependency >
41
+ <groupId >org.springframework.boot</groupId >
42
+ <artifactId >spring-boot-starter-data-jpa</artifactId >
43
+ </dependency >
41
44
<dependency >
42
45
<groupId >com.h2database</groupId >
43
46
<artifactId >h2</artifactId >
48
51
<artifactId >spring-boot-starter-test</artifactId >
49
52
<scope >test</scope >
50
53
</dependency >
51
- </dependencies >
54
+ </dependencies >
52
55
53
56
<build >
54
57
<plugins >
Original file line number Diff line number Diff line change
1
+ package com .hhimanshu .business ;
2
+
3
+ import com .hhimanshu .persistence .entities .Author ;
4
+ import com .hhimanshu .persistence .repositories .AuthorRepository ;
5
+ import java .util .stream .Stream ;
6
+ import java .util .stream .StreamSupport ;
7
+ import org .springframework .beans .factory .annotation .Autowired ;
8
+ import org .springframework .stereotype .Component ;
9
+
10
+ @ Component
11
+ public class AuthorsManager {
12
+
13
+ private AuthorRepository authorRepository ;
14
+
15
+ @ Autowired
16
+ public AuthorsManager (AuthorRepository authorRepository ) {
17
+ this .authorRepository = authorRepository ;
18
+ }
19
+
20
+ public Stream <Author > getAllAuthors () {
21
+ return StreamSupport .stream (authorRepository .findAll ().spliterator (), true );
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ package com .hhimanshu .persistence .entities ;
2
+
3
+ import javax .persistence .Column ;
4
+ import javax .persistence .Entity ;
5
+ import javax .persistence .GeneratedValue ;
6
+ import javax .persistence .GenerationType ;
7
+ import javax .persistence .Id ;
8
+
9
+ @ Entity (name = "authors" )
10
+ public class Author {
11
+
12
+ @ Id
13
+ @ GeneratedValue (strategy = GenerationType .AUTO )
14
+ private Integer id ;
15
+
16
+ @ Column (nullable = false , unique = true )
17
+ private String name ;
18
+
19
+ protected Author () {
20
+ // for JPA
21
+ }
22
+
23
+ public Author (Integer id , String name ) {
24
+ this .id = id ;
25
+ this .name = name ;
26
+ }
27
+
28
+ public Integer getId () {
29
+ return id ;
30
+ }
31
+
32
+ public String getName () {
33
+ return name ;
34
+ }
35
+
36
+ @ Override
37
+ public String toString () {
38
+ return "Author{" +
39
+ "id=" + id +
40
+ ", name='" + name + '\'' +
41
+ '}' ;
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ package com .hhimanshu .persistence .repositories ;
2
+
3
+ import com .hhimanshu .persistence .entities .Author ;
4
+ import org .springframework .data .repository .CrudRepository ;
5
+
6
+ public interface AuthorRepository extends CrudRepository <Author , Integer > {
7
+
8
+ }
Original file line number Diff line number Diff line change
1
+ package com .hhimanshu .business ;
2
+
3
+
4
+ import static org .junit .Assert .assertEquals ;
5
+ import static org .junit .Assert .assertFalse ;
6
+
7
+ import com .hhimanshu .persistence .entities .Author ;
8
+ import java .util .List ;
9
+ import java .util .stream .Collectors ;
10
+ import org .junit .Test ;
11
+ import org .junit .runner .RunWith ;
12
+ import org .springframework .beans .factory .annotation .Autowired ;
13
+ import org .springframework .boot .test .context .SpringBootTest ;
14
+ import org .springframework .test .context .junit4 .SpringRunner ;
15
+
16
+ @ RunWith (SpringRunner .class )
17
+ @ SpringBootTest
18
+ public class AuthorsManagerTest {
19
+
20
+ @ Autowired
21
+ private AuthorsManager authorsManager ;
22
+
23
+ @ Test
24
+ public void getAllAuthorsReturnDataFromDatabase () throws Exception {
25
+ List <Author > authors = authorsManager .getAllAuthors ().collect (Collectors .toList ());
26
+ assertFalse (authors .isEmpty ());
27
+ assertEquals (5 , authors .size ());
28
+
29
+ // If you want to compare all the authors to what we inserted in '02-insert-data-authors.xml'
30
+ // authors.forEach(System.out::println);
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments