Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SPOCK - All @Shared Variables are NULL

Here is my test class:

import grails.test.mixin.*
import spock.lang.Specification
import spock.lang.Unroll
import spock.lang.*
import groovy.util.logging.Slf4j

@Slf4j
@Mock([PromoRule, PromoCode, SecUser])
@TestFor(PromoService)
class PromoServiceSpec extends Specification {

@Shared testUser
@Shared testCode
@Shared testRule

def setup() {

}

@Unroll
def 'valid promo codes - #user, #code'() {
    given:
    testRule = new PromoRule(
            name : "ZTEST",
            receiverAmount : 5,
            receiverAmountType : PromoRule.AmountType.DOLLARS,
            senderAmount : 0,
            senderAmountType : PromoRule.AmountType.DOLLARS,
            receiverPointsAmount : null,
            receiverPointsAmountType : null,
            receiverMaxUse : null,
    )
    testRule.save(flush:true, failOnError:true)

    testUser = new SecUser(
            id: 1,
            version: 0,
            accountExpired: false,
            accountLocked: false,
            age: 9000,
            balance: 100,
            dateCreated: new Date(),
            emailVerified: true,
            enabled: true,
            firstName: 'Sir',
            lastName: 'Buttocks',
            lastUpdated: new Date(),
            lockedBalance: 0,
            username: "1",
            staff: false,
            displayName: 'sir_buttocks',
            usernameChosen: true,
            depositMade: true,
            depositOfferRecentlySeen: false,
            pin: null
    )
    testUser.save(flush: true, failOnError: true)

    testCode = new PromoCode(
            rule : testRule,
            code : "3",
            senderId : 1,
    )
    testCode.save(flush:true, failOnError:true)

    expect:
    service.isValidPromoCode(user, code) == value

    where:
    user | code || value
    testUser | testCode || true
}

}

When I run this test, I get the following:

| Failure:  valid promo codes - null, null(skillz.PromoServiceSpec)
|  Condition not satisfied:

service.isValidPromoCode(user, code) == value
|       |                |     |     |  |
|       false            null  null  |  true
skillz.PromoService@20e0e9d5         false

I have tried a ton of different configurations and layouts, all of them either getting me a null pointer exception (to the variable itself) or a null value for the variable.

Doing all the definitions at static variables didn't change anything either, same result as using @Shared.

I've tried mocking these as well, but I always get null exceptions when trying to execute .Mock() for the class...

Thanks!!

like image 409
ChronaldMcDonald Avatar asked Jan 15 '14 22:01

ChronaldMcDonald


1 Answers

I'm not exactly sure what you are trying to achieve here, but the where block is evaluated before (the rest of) the method is first entered, and at that time, your shared variables are null. You'd have to set them earlier, e.g. in a setupSpec (not setup) method.

like image 86
Peter Niederwieser Avatar answered Oct 21 '22 12:10

Peter Niederwieser