Grails domain class unit test errors - grails

I wrote the following domain class with its unit test. When I run the tests, I get the error message
No such property: admittedMobileUser. When I comment that line in domain class I get the same problem on other fields. Does anyone knows how to solve it?
class MobileUser {
String userName
String userDevice="Android"
Integer userCluster
Boolean admittedMobileUser
Date lastTimeUpdatedUserSpaceTime=new Date()
byte[] userSpaceTimeXml
static constraints = {
userName blank:false, unique:true
userDevice blank:false
userCluster validator : {val-> return val > 0}
admittedMobileUser
lastTimeUpdatedUserSpaceTime
userSpaceTimeXml maxSize:1024*1024
}
String toString(){
return "${userName}_${userCluster}"
}
}

remove such rows from constraints :
....
admittedMobileUser
lastTimeUpdatedUserSpaceTime
....
or add minimum one constraint to each!

Related

How to set the constraints of dynamic scaffolding project?

I'm very new to grails web application! Here is my domain class
package imocha.project
class Feedback {
String name
String email
String type
String remark
static constraints = {
name(blank:true)
email(blank:true)
type(blank:false)
remark(blank:false)
}
}
In my case, I just want to set only the type and remark can't be blank! But at the end, it set all for me.
The reason for this is the default behavior for properties is nullable: false in a Grails project. Alter your constraints as follows:
package imocha.project
class Feedback {
String name
String email
String type
String remark
static constraints = {
name(nullable:true, blank:true)
email(nullable:true, blank:true)
type(blank:false)
remark(blank:false)
}
}

Creating Domain Relationships in Grails returns unable to resolve class error

Good day to all. I am very new in using grails and I have followed several tutorials for beginners using grails until I come up in creating domain relationships. However, I got stuck with this problem right now. I have 3 domain classes namely, todo, category and user. And as I defined their relationships, it returns me an error saying unable to resolve class. Please see my codes below. Please help. Thank you so much.
Todo.groovy Class
package todoScaff
class Todo {
String name
String note
Date createDate
Date dueDate
Date completedDate
String priority
String status
User owner
Category category
static belongsTo = [User, Category]
static constraints = {
name(blank:false)
createDate()
priority()
status()
note(maxsize:1000, nullable:true)
completedDate(nullable:true)
dueDate(nullable:true)
}
String toString() {
name
}
}
Category.groovy Class
package categoryScaff
class Category {
String name
String description
User user
static belongsTo = User
static hasMany = [todos: Todo]
static constraints = {
name(blank:false)
}
String toString(){
name
}
}
User.groovy Class
package userScaff
class User {
String userName
String fname
String lname
static hasMany = [todos: Todo, categories: Category]
static constraints = {
userName(blank:false, unique:true)
fname(blank:false)
lname(blank:false)
}
String toString(){
"$lname, $fname"
}
}
Since you've placed your domain classes in different packages, you must import the classes at the head of the file.
package categoryScaff
import todoScaff.Todo
import userScaff.User
class Category {
The same needs to happen in your other domain classes that reference classes outside the current package.

BootStrap.groovy content doesn't update the content

I'm new to Grails and study it via InfoQ's "Getting Started With Grails" book.
Running through it I've faced a problem with the BootStrap.groovy file:
I've edited it as the book says.
Run-app it - and the data I've entered into BootStrap.groovy has been shown.
I've updated the bootstrapping values but they are not changed.
No errors are shown in the console.
What I've tried:
exchanging def's places;
set Runner's class value to nullables;
changed .save(failOnError:true) to .save(flush:true).
Even after I've changed the Race class' bootstrapping values it looks like it hasn't been changed.
Looks like the value is taken from somewhere else than the BootStrap.groovy?
I use Grails 2.2.1 with a PostgreSQL DB.
The classes' code is below:
package racetrack
class Runner {
static constraints = {
firstName(blank:false)
lastName(blank:false)
dateOfBirth(nullable:true)
gender(inList:["M","F"])
address(nullable:true)
city(nullable:true)
state(nullable:true)
zipcode(nullable:true)
email(email:true)
}
static hasMany = [registrations:Registration]
/*static mapping = {
sort "email"
}*/
String firstName
String lastName
Date dateOfBirth
String gender
String address
String city
String state
String zipcode
String email
String toString(){
"${firstName} ${lastName} (${email})"
}
}
and
package racetrack
class BootStrap {
def init = { servletContext ->
def begun = new Runner(firstName:"Marathon",
lastName:"Runner",
dateOfBirth:"",
gender:"M",
address:"",
city:"",
state:"",
zipcode:"",
email:"me#me.ru"
)
begun.save(flush:true)
def zabeg = new Race(name:"Run SPB",
startDate:new Date() + 360*30,
city:"Moscow",
state:"Moscow",
cost:10.0,
distance:42.0,
maxRunners:10)
zabeg.save(flush:true)
}
def destroy = {
}
}
EDIT: could this be happening due to any generate-* scripts been run?
Race.groovy:
package racetrack
class Race {
static hasMany = [registrations:Registration]
String name
Date startDate
String city
String state
BigDecimal distance
BigDecimal cost
Integer maxRunners
static constraints = {
name(blank:false, maxSize:50)
startDate(validator: {
return (it >= new Date())
}
)
city()
state(inList:["Moscow", "Volgograd", "SPb", "NN"])
distance(min:0.0)
cost(min:0.0, max:100.0)
maxRunners(min:0, max:100000)
}
static mapping = {
sort "startDate"
}
BigDecimal inMiles(){
return distance*0.6214
}
String toString(){
return "${name}, ${startDate.format('MM/dd/yyyy')}"
}
}
BootStrap should be in grails-app/conf instead. Refer this.
Try to print the errors of the new instance you save by adding
print begun.errors
See if somethings comes up.
You need to remove
def index() { }
from the controllers.
I had the same issue. Now it is fixed.

Grails Build Test Data Plugin - MissingMethodException when calling DomainObject.build()

The Crux
I am getting the following error in my unit test when calling MyDomainObject.build() via the Build Test Data plugin:
The Exception
groovy.lang.MissingMethodException: No signature of method: us.maponline.pesticide.PesticideProfile.addToApplicators() is applicable for argument types: (us.maponline.pesticide.PesticideApplicator) values: [us.maponline.pesticide.PesticideApplicator : null]
Possible solutions: getApplicators()
at grails.buildtestdata.handler.NullableConstraintHandler.addInstanceToOwningObjectCollection(NullableConstraintHandler.groovy:121)
at grails.buildtestdata.handler.NullableConstraintHandler.populateDomainProperty(NullableConstraintHandler.groovy:88)
at grails.buildtestdata.handler.NullableConstraintHandler.handle(NullableConstraintHandler.groovy:17)
at grails.buildtestdata.DomainInstanceBuilder.createMissingProperty(DomainInstanceBuilder.groovy:187)
at grails.buildtestdata.DomainInstanceBuilder.populateInstance(DomainInstanceBuilder.groovy:147)
at grails.buildtestdata.DomainInstanceBuilder.build(DomainInstanceBuilder.groovy:124)
at grails.buildtestdata.DomainInstanceBuilder.build(DomainInstanceBuilder.groovy:123)
at grails.buildtestdata.BuildTestDataService$_addBuildMethods_closure1.doCall(BuildTestDataService.groovy:25)
at us.maponline.pesticide.PesticideLogServiceTests.testSaveLog(PesticideLogServiceTests.groovy:20)
| Completed 1 unit test, 1 failed in 5289ms
per the stack trace, this is happening within the buildtestdata plugin code. It seems that my class, PesticideApplicator, is null when it is being added to the PesticideProfile class.
How is it possible that the class I'm asking to be built is null when being passed to the PesticideProfile?
Source Code
Test Case
#TestFor(PesticideLogService)
#Build([PesticideLog,PesticideApplicator,PesticideProfile])
class PesticideLogServiceTests
{
void testSaveLog() {
PesticideApplicator applicator = PesticideApplicator.build()
def result = service.createLog(applicator, new Date())
result.errors.each {
log.info "got an error. field = $it.field, message:$it.defaultMessage, rejected value = $it.rejectedValue "
}
assert result: 'no result returned'
assert result.success: 'save failed'
assert result.result instanceof PesticideLog: "result was not PesticideLog"
assert applicator.user.pesticideLogs.size() > 0 : 'expected at least on log to be created.'
}
}
PesticideProfileLog
class PesticideProfile
{
def User user
String companyName
static constraints = {
}
static belongsTo = User
static hasMany = [sites: PesticideSite, applicators: PesticideApplicator]
}
PesticideApplicator
class PesticideApplicator
{
String firstName
String lastName
String company
PesticideApplicatorLicense licenseType
Phone phoneNumber
static belongsTo = [profile:PesticideProfile]
static constraints = {
company blank: false, maxSize: 55
firstName blank: false, maxSize: 55
lastName blank: false, maxSize: 100
phoneNumber nullable: true
}
static mapping = {
licenseType length: 55
}
def getUser(){
profile?.user
}
}
Thanks for all your help!
The issue is caused by the build test data plugin attempting to set the value of the user in the PesticideApplicator. The problem is that getUser() isn't a field, it's just a utility helper:
...
def getUser(){
profile?.user
}
...
Removing getUser() from the PesticideApplicator solved the problem.
That said, I'd still like a helper method to access the user (good to not let my code know about the innards of another class). Marking the method #Transient didn't work; the error still appeared. Short of renaming the method, how can I instruct the build test data plugin to ignore this getter?
Thanks!

How to create Domain class Instance in Grails?

I have two Domain class in grails
class Quotation {
static searchable =true
static constraints = {
referenceNo(nullable:true)
dateOfRequest(validator: {return (it <= new Date())})
number(blank:false)
estimatedHours(nullable:true, blank:false, min:0)
}
int referenceNo;
Date dateOfRequest;
String number="11-120001Q01";
int estimatedHours;
String toString(){
return number
}
}
class Project {
static constraints = {
projectID(blank:false,nullable:false)
number(blank:true,nullable:true)
projectManager(blank:false)
teamLeader(blank:false)
teamMembers(blank:false)
sizeOfTeam(blank:false)
status(inList:["active", "closed"])
}
String projectID
Quotation number
String projectManager
String teamLeader
String teamMembers
Integer sizeOfTeam
String status
}
i want to print number and estimatedHours from Quotation class to be in Project class but it is only showing number.Can any one please tell me how to create an instance of Quotation in Project class and call the getter and setter methods of Quotation class?

Resources