Skip to content

Commit de2f1b4

Browse files
author
Mariusz Kuligowski
committed
Add integration tests for StudentService
1 parent e29c0c5 commit de2f1b4

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.grails
2+
3+
import grails.testing.mixin.integration.Integration
4+
import grails.gorm.transactions.Rollback
5+
import spock.lang.Specification
6+
import org.hibernate.SessionFactory
7+
8+
@Integration
9+
@Rollback
10+
class StudentIntegrationSpec extends Specification {
11+
12+
StudentService studentService
13+
SessionFactory sessionFactory
14+
15+
private Long setupData() {
16+
new Student(firstName: 'John',lastName: 'Doe').save(flush: true, failOnError: true)
17+
new Student(firstName: 'Max',lastName: 'Foo').save(flush: true, failOnError: true)
18+
Student student = new Student(firstName: 'Alex',lastName: 'Bar').save(flush: true, failOnError: true)
19+
student.id
20+
}
21+
22+
void "test get"() {
23+
Long id = setupData()
24+
25+
expect:
26+
Student student = studentService.get(id)
27+
student.firstName == 'Alex'
28+
student.lastName == 'Bar'
29+
}
30+
31+
void "test list"() {
32+
setupData()
33+
34+
when:
35+
List<Student> studentList = studentService.list()
36+
37+
then:
38+
studentList.size() == 3
39+
studentList[0].lastName == 'Doe'
40+
studentList[1].lastName == 'Foo'
41+
studentList[2].lastName == 'Bar'
42+
}
43+
44+
45+
void "test delete"() {
46+
Long id = setupData()
47+
48+
expect:
49+
studentService.list().size() == 3
50+
51+
when:
52+
studentService.delete(id)
53+
sessionFactory.currentSession.flush()
54+
55+
then:
56+
studentService.list().size() == 2
57+
}
58+
59+
void "test save"() {
60+
when:
61+
Student student = new Student(firstName: 'John',lastName: 'Doe')
62+
studentService.save(student)
63+
64+
then:
65+
student.id != null
66+
}
67+
}

0 commit comments

Comments
 (0)