Using Geb Browser object from Grails test phase events - grails

I have a Grails event handler like this in scripts/_Events.groovy
eventTestPhaseStart = {
geb.Browser.drive { go "http://localhost:8080/...." }
}
And grails test-app -functional logs an error
"The path to the driver executable must be set by the webdriver.ie.driver system property; ... "
My GebConfig.groovy is set up to use PhantomJS or Chrome, and my tests themselves pass. So I am guessing that at this point in the process, the Geb config hasn't been read yet.
Is there a way that I could use the Geb Browser object from the test phase start/end events?

Related

Grails 3 - conditionally ignore Spock test

I'd like to use Spock annotation #IgnoreIf({condition}) to ignore test based on given environment variable when running from Intellij IDEA.
I use this in my test: #IgnoreIf({ env.IGNORE_REDIS == 'true' }). And set it in Gradle run configuration as following:
However, the environment property is never set. Is it an IDEA bug or am I missing something in my configuration?
How about this?
#IgnoreIf({ properties.IGNORE_REDIS == 'true' })
For me this works in Spock (not using Grails though).
You need to modify your gradle test task to copy the system properties.
task integrationTest(...) {
systemProperties System.properties // this line passes the systemproperties from gradle to your tests
}
And for your test in Spock:
#IgnoreIf({sys['IGNORE_REDIS']})
//or
#IgnoreIf({sys.IGNORE_REDIS})
//or
#IgnoreIf({sys.IGNORE_REDIS == 'true'})

How to configure the Grails embedded Tomcat to use the file based security realm

I need to create a user/password for the embedded Tomcat, since my app uses JAAS. I found some examples, but they refer to older versions of Grails. I am using 2.3.5.
The link above advises to create users via events, in a Grails script:
eventConfigureTomcat = {tomcat ->
println "Loading users"
tomcat.addUser("user", "password")
tomcat.addRole("user", "group")
}
But when I create a Grails script in the scripts folder, it is pre-populated with this:
includeTargets << grailsScript("_GrailsInit")
target(_Events: "The description of the script goes here!") {
// TODO: Implement script here
}
setDefaultTarget(_Events)
How to add the user in the new version of Grails? Thanks!
You have to add the event in the file scripts/_Events.groovy. If it's not there, just create it. If it's there, just append your event to the existing content.
Make sure to execute stop-app and run-app so that the event is processed. When executing run-app, you should see the output of your println-statement.

Spock Integration Test fails when run with other integration tests-succeeds in isolation using integration:spock

I am adding a first spock integration test to an existing set of tests for a grails 2.1.1 application. The test runs and tests pass when run using:
grails test-app integration:spock CreditServiceSpec
(Yes, everything is in the default package - fixing this is a ton of work that will not be approved...file under technical debt.)
However, when I run all the tests (grails test-app), unit test pass, spock unit tests pass, integration tests pass, but I get the following failure for spock integration:
| Completed 818 integration tests, 0 failed in 104001ms
| Running 1 spock test...
| Failure: CreditServiceSpec
| groovy.lang.GroovyRuntimeException: failed to invoke constructor: public org.codehaus.groovy.grails.test.support.GrailsTestAutowirer(org.springframework.context.ApplicationContext) with arguments: [] reason: java.lang.IllegalArgumentException
at grails.plugin.spock.IntegrationSpec.$spock_initializeSharedFields(IntegrationSpec.groovy:33)
| Completed 0 spock test, 0 failed in 33ms
| Tests PASSED - view reports in /Users/*/projects/GrailsPlugins/DomainServices/target/test-reports
I get the exact same exception whether I run the full test I built or the following, very strip down example:
import grails.plugin.spock.IntegrationSpec
class CreditServiceSpec extends IntegrationSpec {
def setup() {}
def cleanup() {}
public void "sample"() {
setup:"Nothing to do here."
expect:"This is the truest of truths..."
true == true
}
}
I did crack open IntegrationSpec and looked at line 33:
#Shared private autowirer = new GrailsTestAutowirer(applicationContext)
But determining how/why the applicationContext is not being passed in properly is beyond me and, perhaps, is the thrust of my question.
Has anyone encountered this kind of behavior and found a way to get spock integration to play nice with other tests? Thanks.
It looks like Grails 2.1.1 had several issues with Spock tests in the Integration scope. Jeff's comment and Peter's in particular sound like the issue you were having; basically the ApplicationHolder was null or an empty list.
The parent task lists Grails 2.2.5 as the fix version. Any chance you can upgrade to that (or some even later version) and see if the problem persists?
There have also been cases where a simple grails clean has fixed issues like this.
I had a problem with the exact same symptom.
I was using the BuildTestData plugin and used the #Build annotation in a IntegrationSpec, but using the #Build with use a transformation wich extended the #TestFor transformation which is incompatible with the Intengration runtime.
So just remove the #Build annotations and it will run.

TestCase properties access with TestRunner

My testCases have custom properties defined as TestCase properties. When i launch the test in the SoapUI environment the properties value changes, but when i use the testrunner doesn't.
I have a "current" var containing the current user info to use in the testCase. Each time the TestCase executes the current var increases in one but when using the testrunner (in jenkins & soapui) it doesn't. The increase code is the following and it's saved in a Groovy Script contained in the testCase.
def i = context.testCase.getPropertyValue("_current");
if(Integer.parseInt(i)+1 < Integer.parseInt(total)) {
context.testCase.setPropertyValue("_current",String.valueOf(Integer.parseInt(i)+1));
log.info("Current user "+testRunner.testCase.getPropertyValue("_current"));
} else {
context.testCase.testSuite.setPropertyValue("_current","0");
log.info "###__!!! Reached limit of users !!!__###";
}
I think the testRunner doesn't execute the groovy scripts contained in the testCase. How can I make it work?
I figured it out
The TestCase and TestSuite properties are embedded in the XML project file. For the changes to persist save is needed. Just need to add the -S flag in the testrunner command.

Why is SpringSecurityUtils null in this Grails unit test?

In Grails 2.0.4, I'm trying to write a controller unit test which invokes the static SpringSecurityUtils.reauthenticate. The test returns a NullPointerException on that invocation. In a debugger, I can see that none of the Groovy dynamic properties (declaredMethods,etc.) of SpringSecurityUtils are populated.
I do note that when running the tests, the "Configuring Spring Security Core" log message is emitted after the unit-test failure. Here is a sample test:
class ReproTest {
void testSpringSecurityUtils() {
String.valueOf(true) // OK: a public final class from the JDK
URLUtils.isRelativeURL("foo") // OK: a class from another plugin
SpringSecurityUtils.reauthenticate "user", "pw" // fails, NPE
}
}
My initial reaction is that maybe plugins aren't accessible during unit tests, but if so, why is the URLUtils call working? And why does the test get "far enough" to initialize the plugin, but after the tests have completed?
For a unit test the container isn't starting up. No Spring injection or "grails goodness" is happening. You see in the logs that the plugin is initializing after the unit tests run, because it [container] does start for the integration tests. If you want to test the SpringSecurityUtils, although guessing it is already tested properly in the plugin, you would want to write an integration test.

Resources