JAVA-26395 Move grails module to web-modules (#15072)
This commit is contained in:
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.grails
|
||||
|
||||
import grails.testing.mixin.integration.Integration
|
||||
import grails.gorm.transactions.Rollback
|
||||
import spock.lang.Specification
|
||||
import org.hibernate.SessionFactory
|
||||
|
||||
@Integration
|
||||
@Rollback
|
||||
class StudentIntegrationSpec extends Specification {
|
||||
|
||||
StudentService studentService
|
||||
SessionFactory sessionFactory
|
||||
|
||||
private Long setupData() {
|
||||
new Student(firstName: 'John',lastName: 'Doe').save(flush: true, failOnError: true)
|
||||
new Student(firstName: 'Max',lastName: 'Foo').save(flush: true, failOnError: true)
|
||||
Student student = new Student(firstName: 'Alex',lastName: 'Bar').save(flush: true, failOnError: true)
|
||||
student.id
|
||||
}
|
||||
|
||||
void "test get"() {
|
||||
Long id = setupData()
|
||||
|
||||
expect:
|
||||
Student student = studentService.get(id)
|
||||
student.firstName == 'Alex'
|
||||
student.lastName == 'Bar'
|
||||
}
|
||||
|
||||
void "test list"() {
|
||||
setupData()
|
||||
|
||||
when:
|
||||
List<Student> studentList = studentService.list()
|
||||
|
||||
then:
|
||||
studentList.size() == 3
|
||||
studentList[0].lastName == 'Doe'
|
||||
studentList[1].lastName == 'Foo'
|
||||
studentList[2].lastName == 'Bar'
|
||||
}
|
||||
|
||||
|
||||
void "test delete"() {
|
||||
Long id = setupData()
|
||||
|
||||
expect:
|
||||
studentService.list().size() == 3
|
||||
|
||||
when:
|
||||
studentService.delete(id)
|
||||
sessionFactory.currentSession.flush()
|
||||
|
||||
then:
|
||||
studentService.list().size() == 2
|
||||
}
|
||||
|
||||
void "test save"() {
|
||||
when:
|
||||
Student student = new Student(firstName: 'John',lastName: 'Doe')
|
||||
studentService.save(student)
|
||||
|
||||
then:
|
||||
student.id != null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import org.openqa.selenium.chrome.ChromeDriver
|
||||
import org.openqa.selenium.chrome.ChromeOptions
|
||||
import org.openqa.selenium.htmlunit.HtmlUnitDriver
|
||||
|
||||
environments {
|
||||
|
||||
// run via “./gradlew -Dgeb.env=chrome -Dwebdriver.chrome.driver=/Applications/chromedriver iT”
|
||||
chrome {
|
||||
driver = { new ChromeDriver() }
|
||||
}
|
||||
|
||||
// run via “./gradlew -Dgeb.env=chromeHeadless -Dwebdriver.chrome.driver=/Applications/chromedriver iT”
|
||||
chromeHeadless {
|
||||
driver = {
|
||||
ChromeOptions o = new ChromeOptions()
|
||||
o.addArguments('headless')
|
||||
new ChromeDriver(o)
|
||||
}
|
||||
}
|
||||
|
||||
// run via “./gradlew -Dgeb.env=htmlUnit iT”
|
||||
htmlUnit {
|
||||
driver = { new HtmlUnitDriver() }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.grails
|
||||
|
||||
import grails.testing.gorm.DomainUnitTest
|
||||
import grails.testing.web.controllers.ControllerUnitTest
|
||||
import spock.lang.*
|
||||
|
||||
class StudentControllerSpec extends Specification implements ControllerUnitTest<StudentController>, DomainUnitTest<Student> {
|
||||
|
||||
void "Test the index action returns the correct model"() {
|
||||
given:
|
||||
controller.studentService = Mock(StudentService) {
|
||||
list() >> [new Student(firstName: 'John',lastName: 'Doe')]
|
||||
}
|
||||
|
||||
when:"The index action is executed"
|
||||
controller.index()
|
||||
|
||||
then:"The model is correct"
|
||||
model.studentList.size() == 1
|
||||
model.studentList[0].firstName == 'John'
|
||||
model.studentList[0].lastName == 'Doe'
|
||||
}
|
||||
|
||||
void "Test the create action returns the correct model"() {
|
||||
when:"The create action is executed"
|
||||
controller.create()
|
||||
|
||||
then:"The model is correctly created"
|
||||
model.student!= null
|
||||
}
|
||||
|
||||
|
||||
void "Test the show action with a null id"() {
|
||||
given:
|
||||
controller.studentService = Mock(StudentService) {
|
||||
1 * get(null) >> null
|
||||
}
|
||||
|
||||
when:"The show action is executed with a null domain"
|
||||
controller.show(null)
|
||||
|
||||
then:"A 404 error is returned"
|
||||
response.status == 404
|
||||
}
|
||||
|
||||
void "Test the show action with a valid id"() {
|
||||
given:
|
||||
controller.studentService = Mock(StudentService) {
|
||||
1 * get(2) >> new Student(firstName: 'John',lastName: 'Doe')
|
||||
}
|
||||
|
||||
when:"A domain instance is passed to the show action"
|
||||
controller.show(2)
|
||||
|
||||
then:"A model is populated containing the domain instance"
|
||||
model.student instanceof Student
|
||||
and:"And student is John Doe"
|
||||
model.student.firstName == 'John'
|
||||
model.student.lastName == 'Doe'
|
||||
}
|
||||
|
||||
void "Test the delete action with an instance"() {
|
||||
given:
|
||||
controller.studentService = Mock(StudentService) {
|
||||
1 * delete(2)
|
||||
}
|
||||
|
||||
when:"The domain instance is passed to the delete action"
|
||||
request.contentType = FORM_CONTENT_TYPE
|
||||
request.method = 'DELETE'
|
||||
controller.delete(2)
|
||||
|
||||
then:"The user is redirected to index"
|
||||
response.redirectedUrl == '/student/index'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user