How to mock the environment variables to write unit tests for a grails application?

Viewed 17

I have the following controller in which a method returns keywords based on the environment it is running on.

import grails.util.Environment

class SearchController {

    static String currentEnvironment() {
        def curEnvironment
        if (Environment.current == Environment.PRODUCTION){
            curEnvironment = 'prod'
        }
        else {
            curEnvironment = 'alpha'
        }
        return curEnvironment
    }
}

How do I write a unit testing to mock the environment variables? Something like:

void testEnvironment(){
    //mock(grails.util.Environment.current = "PRODUCTION")
    assert controller.currentEnvironment == 'prod'
}

I tried running the test with -Dgrails.env=prod and it works, but this approach is not generic. So I was wondering if I could set the environment variables for the tests or better mock it.

0 Answers
Related