Grails generated controller check after moving from Grails 2.3 to 2.4

When I create a controller and its associated unit test class using grails 2.4.4, I get test crashes even after I set the necessary test data in populateValidParams (). I can fix most errors by simply changing model.personInstance! = Null (for the Person class) to model.person! = Null . I am surprised that this is necessary, but at least it works.

Other test failures are in the save, update, and delete tests and are not executed because response.redirectUrl is null. Retreating through it using the debugger, it seems that closing request.withFormat will remove the substitution operator, and not the form / multipartForm block. I thought that request.contentType = FORM_CONTENT_TYPE in the generated unit test was supposed to make the code fall into the form / multipartForm part of the code.

Here is the code I'm testing for a Person domain class that only requires the firstName and lastName fields ...

Domain Class:

package org.crspta.gohomenotes import groovy.transform.EqualsAndHashCode @EqualsAndHashCode() class Person implements Serializable { User user Date dateCreated Date lastUpdated String firstName String middleName String lastName String nickname String emailAddress String cellPhone String homePhone String workPhone static constraints = { firstName nullable: false middleName nullable: true lastName nullable: false nickname nullable: true emailAddress nullable: true, email: true cellPhone nullable: true homePhone nullable: true workPhone nullable: true user nullable: true } @Override public String toString() { return "${name}" } public String getName() { def name = "$lastName, $firstName" if (nickname) { name += " ($nickname)" } return name } } 

PersonControllerSpec:

 @TestFor(PersonController) @Mock(Person) class PersonControllerSpec extends Specification { def populateValidParams(params) { assert params != null params["firstName"] = 'Firstname' params["lastName"] = 'Lastname' } 

Unit Test A method that fails because response.redirectUrl is null.

  void "Test the save action correctly persists an instance"() { when:"The save action is executed with an invalid instance" request.contentType = FORM_CONTENT_TYPE request.method = 'POST' def person = new Person() person.validate() controller.save(person) then:"The create view is rendered again with the correct model" model.person != null view == 'create' when:"The save action is executed with a valid instance" response.reset() populateValidParams(params) person = new Person(params) controller.save(person) then:"A redirect is issued to the show action" response.redirectedUrl == '/person/show/1' controller.flash.message != null Person.count() == 1 } 

PersonController save () method:

  @Transactional def save(Person personInstance) { if (personInstance == null) { notFound() return } if (personInstance.hasErrors()) { respond personInstance.errors, view:'create' return } personInstance.save flush:true request.withFormat { form multipartForm { flash.message = message(code: 'default.created.message', args: [message(code: 'person.label', default: 'Person'), personInstance.id]) redirect personInstance } '*' { respond personInstance, [status: CREATED] } } } 

Specific error message:

 Condition not satisfied: response.redirectedUrl == '/person/show/1' | | | | null false org.codeh aus.groovy.grails.plugins.testing.GrailsMockHttpServletResponse@ 17134f55 <Click to see difference> at org.crspta.gohomenotes.PersonControllerSpec.Test the save action correctly persists an instance(PersonControllerSpec.groovy:56) 
+5
source share

Source: https://habr.com/ru/post/1209958/


All Articles