How to setup and teardown functional test data in Geb grails - grails

I have many working/passing functional geb/spock tests (each extending GebReportingSpec) that are testing a web application with test data all created from the BootStrap.groovy at the beginning of the functional test suite.
I want to move the test data creation into startup() / teardown() methods within each Spec, well actually I wanted to have them inherit it from a base class but apparently the StepWise has issues with inheritance.
So, at present each of my test spec classes look something like:
#Stepwise
class ExampleSpec extends GebReportingSpec {
def "valid root user logs in"() {
given: "I am at the login page"
to LoginPage
when: "I enter root's credentials"
username = "root"
password = "password"
and: "I click the login button"
loginButton.click()
then: "I am logged in and directed to the welcome page"
at WelcomePage
}
}
Now, my problem is that I can't seem to create a new test (above the first test) that can create test data. Without having a valid given/when/then statement the test doesnt appear to be executed and calling a method from within the existing test also doesnt appear to work. I have looked into the grails-remote-control plugin to help me and I believe this will allow me to successfully envoke closures to setup data but I am not sure on the best mechanism for calling this from within the GebReportSpecs (or some abstract parent).
Below is a brief outline of the kind of thing I want to be able to do, either by making 'setupData()' the first test or by calling that method from within a test... Neither appears to work.
def remote = new RemoteControl()
def setupData() {
def id = remote {
def ShiroUser user = new ShiroUser(username: "root", ...)
user.save()
user.id
}
println(id)
}
.... Tests then follow
Are there any annotations like #before etc that can force these methods to be invokved?
Any suggestions are appreciated.
Solution:
I have accepted dmahapatro's response below at the correct answer, but have also provided an example of my final solution below for those who may find it useful.

(Untested)
GebReportingSpec extends GebSpec which ultimately extends spock.lang.Specification which has Fixture Methods.
You can use them like:
#Stepwise
class ExampleSpec extends GebReportingSpec {
def setupSpec(){
super.setupSpec()
//setup your data
}
def cleanupSpec(){
super.cleanupSpec()
//I do not think you would need anything else here
}
def "This is test 1"(){
}
def "This is test 2"(){
}
}
You cannot use setup as one of your test method because the sate is not maintained for a single test case. It goes like this:-
setup called -> test1 -> teardown called
setup called -> test2 -> teardown called
setup called -> test3 -> teardown called
.........

## Solved ##
Thanks to dmahapatro (and erdi). I specifically glossed over setupSpec() and cleanup() as they are private in GebReportingSpec.
Just for completion sake I am going to post a simplified version of my final solution using the grails remote control plugin just in-case it helps anyone else. The only thing to note is that the setup/teardown appears to be called once per Spec, not before each test. Which for me is actually preferably as my test data is quite complex and takes time to be created. So you have a set of test data from the Spec which is modified through the tests in the Spec and then finally cleared down before your next Spec is executed.
#Stepwise
class TestDataBaseSpec extends GebReportingSpec {
protected void createTestUsers() {
def remote = new RemoteControl()
def created = remote {
def createUser = { name, roles, pwHash ->
def user = new ShiroUser(username: name, passwordHash: pwHash, passwordSetDate: new Date())
roles.each { user.addToRoles(it) }
user.save(failOnError: true)
return user
}
createUser("root", [ShiroRole.findByName("base_user")], pwHash)
// .. repeat for more
}
}
protected void deleteTestUsers() {
def remote = new RemoteControl()
def created = remote {
ShiroUser.findAll().each {
it.delete(flush: true)
}
return true
}
}
}
#Stepwise
class ExampleSpec extends TestDataBaseSpec {
def setupSpec() {
super.createTestUsers()
}
def cleanupSpec() {
super.deleteTestUsers()
}
def "valid root user logs in"() {
given: "I am at the login page"
to LoginPage
when: "I enter root's credentials"
username = "root"
password = "password"
and: "I click the login button"
loginButton.click()
then: "I am logged in and directed to the welcome page"
at WelcomePage
}
}

Related

"Cannot invoke method on null object" when injecting service into a controller

I have created a brand new Grails 4.0.0 app and created a domain / controller using the grails cmd. I've also created a simple service that returns "Hello World" to the controller, which then renders this to the screen. However I get "Cannot invoke method on null object" when trying to call the service method - seems like the dependency injection isn't working properly.
I've tried declaring the service using "def", I've also tried declaring by class name - neither of which seem to work.
package uk.org.pmms
import grails.gorm.transactions.Transactional
#Transactional
class HelloWorldService {
def hello() {
return "Hello World"
}
}
package uk.org.pmms
class ClientController {
//static scaffold = Client
def helloWorld
def show(Long id){
Client clientInstance = Client.get(id)
respond ("client": clientInstance, "message": helloWorld.hello())
}
}
I expect the controller to return the clientInstance data and a string "Hello World" which are displayed on a GSP page.
When I remove the "message:" part of the respond statement it displays the client information correctly so it is definitely just the service call that is the problem.
The name of the bean created for your service would be helloWorldService
class ClientController {
def helloWorldService // <--- corrected bean name for auto wire by name.
def show(Long id){
Client clientInstance = Client.get(id)
respond ("client": clientInstance, "message": helloWorldService.hello())
}
}

How to access external website using Geb

I am automating my project using Geb and Groovy. For Example: I am logging into Gmail and I define different Pages as - Inbox, Sent Item, Trash, Drafts, etc. Now in my spec I wanted to access an external website like yahoomail. How can I define it the spec to access an external Webiste.
I use "go" to navigate to yahoo mail as below in my spec
then: "I go to Yahoo mail page"
go "https://login.yahoo.com/"
and: "Signing into Yahoo mail "
at YahooLoginPage
In the YahooLoginPage.groovy it is not finding the Next button which I define as static at
static at = { $("#login-signin") }
Error message I get :
Condition not satisfied:
go "https://login.yahoo.com/"
|
null
Is there any other way to do this?
The problem is that you are using a method which has a void return type in a then: Spock block. Every statement is asserted in then: blocks and that method call evaluates to null because of its return type and hence the failure you're getting.
Basically you should not use Geb's go() method in a then: - use it in in a given: or when: block instead.
I think Erdi's and Jeff's answers above nail it, but since I was curious to find out if it was possible and for an example of how working code might look I went ahead and built a freestanding groovy script which runs a geb spec. The below script enters a username in the yahoo login flow and hits the next button.
#Grapes([
#Grab("org.gebish:geb-spock:2.1"),
#Grab("org.spockframework:spock-core:1.1-groovy-2.4"),
#Grab("org.seleniumhq.selenium:selenium-htmlunit-driver:2.52.0"),
#GrabExclude('org.codehaus.groovy:groovy-all')
])
import geb.*
import geb.spock.*
import spock.util.EmbeddedSpecRunner
import java.util.logging.*
import org.w3c.css.sac.ErrorHandler
import com.gargoylesoftware.htmlunit.SilentCssErrorHandler
new EmbeddedSpecRunner().runClass(YahooSpec)
class YahooSpec extends GebReportingSpec {
def setup() {
// disable css errors output - don't do this for real tests
browser.driver.webClient.cssErrorHandler = new SilentCssErrorHandler()
}
def "should be able to enter username at yahoo"() {
when: "I go to Yahoo mail page"
to YahooLoginPage
then: "there should be a button with value 'Next'"
nextButton.value() == "Next"
when: "I enter a username and click next"
username = "BobbaFett"
nextButton.click()
then: "I should end up at the password page"
at YahooPasswordPage
greeting.text() == "Hello BobbaFett"
}
}
class YahooPasswordPage extends Page {
static url = "https://login.yahoo.com/account/challenge/password"
static at = { title.trim() == "Yahoo" }
static content = {
greeting { $("h1", class: "username")}
}
}
class YahooLoginPage extends Page {
static url = "https://login.yahoo.com/"
static at = { title == "Yahoo - login" }
static content = {
username { $("input#login-username")}
nextButton(to: YahooPasswordPage) { $("input#login-signin") }
}
}
saving the above in a file test.groovy and running:
~> groovy test.groovy
executes the spec. It should be noted that the first run will take some time as the script is downloading dependencies. It should also be noted that using a username which does not exist will break the test as the test assumes that yahoo will send you to the password page after hitting next.
Tested on:
Groovy Version: 2.4.15 JVM: 1.8.0_161 Vendor: Oracle Corporation OS: Mac OS X

How to use g.formatNumber in grails Service class

I want to use g.formatNumber in service, I have tried a below method, Which i got online. This is not working, its giving me the error "Cannot invoke method formatNumber() on null object", The code is below
import org.springframework.beans.factory.InitializingBean
class MyService implements InitializingBean {
boolean transactional = false
def gspTagLibraryLookup // being automatically injected by spring
def g
public void afterPropertiesSet() {
g = gspTagLibraryLookup.lookupNamespaceDispatcher("g")
assert g
}
def getFormattedNumber(){
def number = g.formatNumber(number: 5000,234 , type: "number" , maxFractionDigits: 2)
return number
}
}
How to do this.
I want to use g.formatNumber in service
Rather than jumping through the hoops you need to use a taglib within a service, it would be simpler to just use java.text.NumberFormat directly
NumberFormat format = NumberFormat.getNumberInstance()
format.maximumFractionDigits = 2
def number = format.format(5000.234)
If the service method is being called from a web request handling thread then you may wish to use the LocaleContextHolder to get the correct locale for the current web request, rather than just using the server's default.
This should work
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib');
You will of course need grailsApplication injected by defining it ala
def grailsApplication

Not being able to inject services into AtmosphereHandler

I am running Grails 2.2.1.
I have a Grails service, which acts as an atmosphere handler..
Refer this link: https://bitbucket.org/bgoetzmann/grails-atmosphere-plugin/wiki/Home
I am trying to use spring security service inside this, in your simple way, ie, by injecting it via def springSecurityService.
But when a service hits the handler,
springSecurityService.getCurrentUser() returns null.
User is logged in, I am able to get him in my controllers. I think the service is not injecting somehow.
After some research I came across this question
Injecting service into Grails Atmosphere Handler class
but both answers are outdated...
Please help!
EDIT: my service goes like:
AtmosphereRequest req = r.getRequest();
if (req.getMethod().equalsIgnoreCase("GET")) {
log.info "got get, and suspending."
r.suspend();
} else if (req.getMethod().equalsIgnoreCase("POST")) {
def data = req.getReader().readLine().trim()
log.info "got some data:\n $data"
if (data == "GET_NEARBY"){
log.info "finding nearby..."
def user = springSecurityService.getCurrentUser()
log.info "user is $user" //USER IS NULL HERE
//...some logic
}
}
Try this:
def ctx = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
def springSecurityService = ctx. springSecurityService
def currentUser = springSecurityService.currentUser
now you should be able to use springSecurityService

Grails service injection into integration test

I have a very simple Grails Service:
class UserService {
def returnHi() { return "Hi" }
}
I'm trying to get access to the service in an integration test, like this:
def testService() {
UserService userService
assertEquals( "Hi", userService.returnHi() )
}
Why do I get the failure:
java.lang.NullPointerException: Cannot invoke method returnHi() on null object?
Thanks for your time
It's enough to put 'def userService' as your class field instead of putting in inside of the method. In integration tests, beans are injected the same as in controllers, services and other beans.
Do something like:
class MyTests {
def userService
void serviceTest(){
assert userService.returnHi(), 'Hi'
}
}
P.S. Make sure the name of the service is correct and written in camelCase.
Add the following lines to the integration test file:
import org.codehaus.groovy.grails.commons.ApplicationHolder as AH
def userService = AH.application.mainContext.userService
as described here: Service is not getting injected into Grails domain class , Grails 2.0.0 and 2.0.3

Resources