I'm using a MongoDB database in a Grails 3 application and I'm having some issues while doing some unitary tests.
This is my domain class
@ToString(includeFields = true, includeNames = true, excludes = "dateCreated, lastUpdated, metaClass, sites")
@EqualsAndHashCode(excludes = "site")
class Campaign {
static mapWith = "mongo"
ObjectId id
String name
Boolean manualActive
Date startDate
Date endDate
static belongsTo = [site:Site]
static constraints = {
name size: 1..255, blank:false
manualActive nullable:true, validator: {val, obj->
(val != null && obj.startDate == null && obj.endDate == null) || (val == null && obj.startDate != null)
}
startDate nullable:true, validator: {val, obj->
(val != null && obj.manualActive == null) || (val == null && obj.manualActive != null)
}
endDate nullable:true, validator: {val, obj ->
(val != null && obj.startDate != null) || (val == null)
}
}
}
I'm also using build-test-data plugin to ease data creation for the tests and if I try something like the following, I receive a StackoverflowException:
@TestFor(CampaignService)
@Mock([Site, Campaign])
@Build([Site, Campaign])
class CampaignServiceSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "Test that isSameCampaign throws a ForbiddenException if no site is given"() {
given:
def campaign = Campaign.build(id:new ObjectId(), manualActive:true)
def site = null
when:
service.isSameSite(site, campaign)
then:
thrown ForbiddenException
}
void "Test that isSameCampaign throws a ForbiddenException if it mismatches from the campaign"() {
given:
def site = Site.build(id:new ObjectId(), )
def campaign = Campaign.build(id:new ObjectId(), manualActive:true)
when:
service.isSameSite(site, campaign)
then:
thrown ForbiddenException
}
void "Test that isSameCampaign does NOT throw a ForbiddenException if no campaign is given"() {
given:
def site = Site.build(id:new ObjectId(), )
def campaign = Campaign.build(id:new ObjectId(), site:site,manualActive:true)
when:
service.isSameSite(site, campaign)
then:
notThrown ForbiddenException
}
}
But if I remove de @ToString() annotation from the domain class and implement my own toString method the tests are run ok. Any ideas why it's happening this stackoverflowException? May it be related with the ObjectId field?
Aucun commentaire:
Enregistrer un commentaire