Using standalone GORM for testing

Recently I was stuck with persistence testing. I like to write simple unit tests and run them from my favorite IDE (Intellij IDEA forever!), but there was no luck with GORM Criterias testing. My case is simple: I have Grails Service which using criteria to fetch some objects from the database. Testing with mockDomain() won’t work since you need Hibernate support. Of course you could still write integration tests as described in Grails tutorial, but I just didn’t want to.
First off I looked through google’s top 10 about the subject, but after few hours of trials I was still where I started.
I decided to look on Grails sources and (voilà!) I found HibernateCriteriaBuilderTests. Which has tested exactly what I messed with.
I’m not going to share all things I’ve done until I got finally working abstract test case (based on AbstractGrailsHibernateTests) . You should just derive your test from test case below and use Grails domain objects in your test as usual. Also I’ll provide two classes MockGrailsPluginManager and MockHibernateGrailsPlugin. Both of them were taken as is from Grails test sources.

Recently I was stuck with persistence testing. I like to write simple unit tests and run them from my favorite IDE (Intellij IDEA forever!), but there was no luck with GORM Criterias testing. My case is simple: I have Grails Service which using criteria to fetch some objects from the database. Testing with mockDomain() won’t work since you need Hibernate support. Of course you could still write integration tests as described in Grails tutorial, but I just didn’t want to.

First off I looked through google’s top 10 about the subject, but after few hours of trials I was still where I started.

I decided to look on Grails sources and (voilà!) I found HibernateCriteriaBuilderTests. Which has tested exactly what I messed with.

I’m not going to share all things I’ve done until I got finally working abstract test case (based on AbstractGrailsHibernateTests) . You should just derive your test from test case below and use Grails domain objects in your test as usual. Also I’ll provide two classes MockGrailsPluginManager and MockHibernateGrailsPlugin. Both of them were taken as is from Grails test sources.

AbstractPersistenceTestCase

package com.grailsgeek

import com.grailsgeek.MockHibernateGrailsPlugin
import grails.test.GrailsUnitTestCase
import groovy.lang.ExpandoMetaClass
import groovy.lang.ExpandoMetaClassCreationHandle
import groovy.lang.GroovyClassLoader
import groovy.lang.GroovySystem
import org.codehaus.groovy.grails.commons.ApplicationHolder
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.spring.DefaultRuntimeSpringConfiguration
import org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator
import org.codehaus.groovy.grails.plugins.datasource.DataSourceGrailsPlugin
import org.codehaus.groovy.grails.plugins.i18n.I18nGrailsPlugin
import org.codehaus.groovy.grails.support.MockApplicationContext
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.springframework.context.ApplicationContext
import org.springframework.context.support.StaticMessageSource
import org.springframework.orm.hibernate3.SessionFactoryUtils
import org.springframework.orm.hibernate3.SessionHolder
import org.springframework.transaction.support.TransactionSynchronizationManager
import org.codehaus.groovy.grails.plugins.*

abstract class AbstractPersistenceTestCase extends GrailsUnitTestCase {
    GroovyClassLoader gcl = new GroovyClassLoader(this.getClass().classLoader)
    MockApplicationContext ctx;
    ApplicationContext appCtx
    SessionFactory sessionFactory
    Session session

    protected void setUp() {
        super.setUp();

        ExpandoMetaClass.enableGlobally()

        GroovySystem.metaClassRegistry.metaClassCreationHandle = new ExpandoMetaClassCreationHandle();

        gcl.parseClass('''
dataSource {
	pooled = true
	driverClassName = "org.hsqldb.jdbcDriver"
	username = "sa"
	password = ""
    dbCreate = "create-drop" // one of 'create', 'create-drop','update'
    url = "jdbc:hsqldb:mem:testDB"
}
hibernate {
    cache.use_second_level_cache=true
    cache.use_query_cache=true
    cache.provider_class='com.opensymphony.oscache.hibernate.OSCacheProvider'
}
''', "DataSource")

        ctx = new MockApplicationContext();

        def classes = getDomainClasses()

        gcl.getLoadedClasses().each {
            classes << it
        }

        def ga = new DefaultGrailsApplication(classes as Class[], gcl)
        def mockManager = new MockGrailsPluginManager(ga)
        ctx.registerMockBean("manager", mockManager)
        PluginManagerHolder.setPluginManager(mockManager)

        def dependentPlugins = [
                DataSourceGrailsPlugin,
                DomainClassGrailsPlugin,
                I18nGrailsPlugin,
                MockHibernateGrailsPlugin
        ].collect { new DefaultGrailsPlugin(it, ga)}

        dependentPlugins.each { mockManager.registerMockPlugin(it); it.manager = mockManager }
        mockManager.doArtefactConfiguration();
        ctx.registerMockBean(PluginMetaManager.BEAN_ID, new DefaultPluginMetaManager());

        ga.initialise()
        ga.setApplicationContext(ctx);
        ApplicationHolder.setApplication(ga)
        ctx.registerMockBean(GrailsApplication.APPLICATION_ID, ga);
        ctx.registerMockBean("messageSource", new StaticMessageSource())

        def springConfig = new DefaultRuntimeSpringConfiguration(ctx, gcl)
        dependentPlugins*.doWithRuntimeConfiguration(springConfig)

        appCtx = springConfig.getApplicationContext()
        dependentPlugins*.doWithApplicationContext(appCtx)

        mockManager.applicationContext = appCtx
        mockManager.doDynamicMethods()

        sessionFactory = appCtx.getBean(GrailsRuntimeConfigurator.SESSION_FACTORY_BEAN);

        if (!TransactionSynchronizationManager.hasResource(sessionFactory)) {
            session = sessionFactory.openSession();
            TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
        }
    }

    abstract List getDomainClasses()

    protected void tearDown() {
        if (TransactionSynchronizationManager.hasResource(this.sessionFactory)) {
            SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(this.sessionFactory);
            org.hibernate.Session s = holder.getSession();
            //s.flush();
            TransactionSynchronizationManager.unbindResource(this.sessionFactory);
            SessionFactoryUtils.releaseSession(s, this.sessionFactory);
        }

        ApplicationHolder.setApplication(null)
        ExpandoMetaClass.disableGlobally()
        PluginManagerHolder.setPluginManager(null)

        super.tearDown();
    }
}

MockGrailsPluginManager

package com.grailsgeek;

import groovy.lang.GroovyClassLoader;
import junit.framework.Assert;
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication;
import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.plugins.exceptions.PluginException;
import org.springframework.core.io.Resource;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map
import org.codehaus.groovy.grails.plugins.AbstractGrailsPluginManager
import org.codehaus.groovy.grails.plugins.GrailsPlugin;

/**
 * @author Graeme Rocher
 * @since 0.4
 *
 */

public class MockGrailsPluginManager extends AbstractGrailsPluginManager {
    private ServletContext servletContext;
    private boolean checkForChangesExpected = false;

    public ServletContext getServletContext() {
        return servletContext;
    }

    public MockGrailsPluginManager(GrailsApplication application) {
		super(application);
		loadPlugins();
	}

	public MockGrailsPluginManager() {
		this(new DefaultGrailsApplication(new Class[0], new GroovyClassLoader()));
	}

	public GrailsPlugin getGrailsPlugin(String name) {
		return this.plugins.get(name);
	}

	public GrailsPlugin getGrailsPlugin(String name, BigDecimal version) {
		return this.plugins.get(name);
	}

	public boolean hasGrailsPlugin(String name) {
		return this.plugins.containsKey(name);
	}

	public void registerMockPlugin(GrailsPlugin plugin) {
		this.plugins.put(plugin.getName(), plugin);
		this.pluginList.add(plugin);
	}

	public void loadPlugins() throws PluginException {
		this.initialised = true;
	}

	public void checkForChanges() {
        Assert.assertTrue(this.checkForChangesExpected);
        this.checkForChangesExpected = false;
    }

	public void doWebDescriptor(Resource descriptor, Writer target) {
		// do nothing
	}

	public void doWebDescriptor(File descriptor, Writer target) {
		// do nothing
	}

	public boolean isInitialised() {
		return true;
	}

    public void refreshPlugin(String name) {
        GrailsPlugin plugin = plugins.get(name);
        if(plugin != null) {
            plugin.refresh();
        }
    }

    public Collection getPluginObservers(GrailsPlugin plugin) {
        throw new UnsupportedOperationException("The class [MockGrailsPluginManager] doesn't support the method getPluginObservers");
    }

    public void informObservers(String pluginName, Map event) {
        // do nothing
    }

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    public void expectCheckForChanges() {
        Assert.assertFalse(this.checkForChangesExpected);
        this.checkForChangesExpected = true;
    }

    public void verify() {
        Assert.assertFalse(this.checkForChangesExpected);
    }
}

MockHibernateGrailsPlugin

package com.grailsgeek

import org.codehaus.groovy.grails.plugins.orm.hibernate.HibernatePluginSupport

class MockHibernateGrailsPlugin {
    def version = grails.util.GrailsUtil.getGrailsVersion()
    def dependsOn = [dataSource: version,
            i18n: version,
            core: version,
            domainClass: version]

    def loadAfter = ['controllers']
    def doWithSpring = HibernatePluginSupport.doWithSpring
    def doWithDynamicMethods = HibernatePluginSupport.doWithDynamicMethods
}

Tags: , , ,

Assert image presence on page with Selenium

Assume you have some images on your page under test, which has some images. You should be sure they are really displayed. Simple validation on image control presence doesn’t solves the problem, because <img> could have invalid source (src attribute). We will enhance our basic test with method which could be used like:

assertImagePresent("imgUnderTestId")

assertImagePresent() method could be used with img component id or with XPath locator to assert that image has nonzero width and height (such technique used because width and height under assertion are real dimensions of target image in already rendered page).

We will enhance our basic SeleniumTest (used as superclass for all integration test cases) again:

public abstract class SeleniumTest extends SeleneseTestCase {
    static String context = "http://localhost:8080/app/"

    public void setUp() {
        setUp context, "*firefox"
    }
    ...
    def assertImagePresent(String imageIdOrXPathLocator) {
        assertTrue Integer.valueOf(getDomAttribute(imageIdOrXPathLocator, "naturalWidth")) > 0
        assertTrue Integer.valueOf(getDomAttribute(imageIdOrXPathLocator, "naturalHeight")) > 0
    }

    def getDomAttribute(String elementIdOrXPathLocator, String attribute) {
        if (elementIdOrXPathLocator.startsWith("//")) {
            def expression = "window.document.evaluate('${StringEscapeUtils.escapeJavaScript(elementIdOrXPathLocator)}', window.document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue['$attribute'];"
            return selenium.getEval(expression)
        } else {
            return selenium.getEval("window.document.getElementById('$elementIdOrXPathLocator')['$attribute'];")
        }
    }
    ...
}

Tags: , , , ,

Adding WebFlow support to Maven driven Grails project

It’s just simple tip, but you could find it useful if you noob in Maven. WebFlow comes with Grails by default, but since we develop with Maven we should include this dependency manually. Just modify your Grails project pom.xml dependencies by including webflow reference:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    ...
    <dependencies>
        ...
        <dependency>
            <groupId>org.grails</groupId>
            <artifactId>grails-webflow</artifactId>
            <version>1.1.1</version>
        </dependency>
        ...
    </dependencies>
    ...
</project>

Tags: , ,

Attaching file to file upload control with Selenium (via JavaScript)

I didn’t found useful way to set file inputs controls in our integration tests written with Selenium. I propose JavaScript way to execute with Selenium RC to achieve this. Finally this should be doable with following code in any Test Case:

setFile "fileInputId", getResourcePath("some photo.jpg")

getResourcePath() – returns path to resource (java/groovy resources meant). You could use any path string instead, but inclusion of resources (test resources) is more convenient, because they become part of your test environment. If you have some problems with java/groovy resources, feel free to ask.

* We will extend our SeleniumTest (basic class for all integration tests with this method:

public abstract class SeleniumTest extends SeleneseTestCase {
    static String context = "http://localhost:8080/app/"

    public void setUp() {
        setUp context, "*firefox"
    }
    ...
    void setFile(String fileInputId, String fileName) {
        selenium.getEval(
            """var imageControl = window.document.getElementById('${fileInputId}');
               imageControl.value='$fileName';
               var event = window.document.createEvent('HTMLEvents');
               event.initEvent('change',true,true);
               imageControl.dispatchEvent(event);
            """)
    }

    String getResourcePath(String resourcePath) {
        getClass().getResource(resourcePath).getPath()
    }
    ...
}

This method uses document model to inject file path into HTML file input control and also generates onChange event, which could be useful if onChange event handler defined for this component.

Tags: , , ,

CSS convention for Grails

As far as our CSS styles grow I found that there should intuitive way to manage CSS inclusions. I’ve decided to follow great Grails fundamental idea and place them by some convention and include automagically.

We found useful to have separate CSS for each view (or controller action if you wish). Convention is simple: place each CSS in the same path as you place your views under /views folder.

For example, we have view: /views/picture/gallery.gsp and want to have gallery.css which contains all styles for this view. We should place it in the same path as our view located, in our example it should be /web-app/css/picture/gallery.css. Now to automatically include such CSSs by convention we should modify our main layout (/views/layouts/main.gsp) next way:

<head>
...
<% if (webRequest.controllerName && webRequest.actionName) { %>
    <link rel="stylesheet" href="${resource(dir: 'css/' + webRequest.getControllerName(), file: webRequest.getActionName() + '.css')}"/>
<% } %>
...
</head>

All further CSSs placed by this convention will be automatically included for each action (which uses main layout) . If you have more layouts you could modify them same way. Anyway their count will be much less than your actions/views count.

Hope this helps. If you have some ideas to improve this approach or you use some other approach, please share :)



                      

Tags: ,

Grails, Selenium, Maven and integration testing

Today I wanna share with you my experience of configuring Selenium RC (with Java client drivers), Maven, Surefire to develop and run integration tests on Grails applications. We will build Grails demo application in Intellij IDEA (with Maven project model behind) and run simple integration test written in Groovy using Selenium RC.

Creating project structure

  • Create “demo” directory in your projects dir an step inside it.
  • Create basic Grails application with Maven by running:
mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.grails -DarchetypeArtifactId=grails-maven-archetype -DarchetypeVersion=1.0 -DgroupId=com.grailsgeek -DartifactId=maven-grails-demo

Fix Grails application pom.xml with tips I described in my previous post.

  • Create integration-test module by running:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.grailsgeek -DartifactId=integration-test

And don’t forget to remove App and AppTest from sources that were generated by Maven.

  • You should stay in your main project dir “demo”. Create multi-module pom.xml in project root dir with following content:
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.grailsgeek</groupId>
    <version>1.0-SNAPSHOT</version>
    <artifactId>demo</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>demo-app</module>
        <module>integration-test</module>
    </modules>
</project>
  1. Now run mvn install from project root directory, and see “BUILD SUCCESSFUL” ;)

Adding necessary dependencies to maven poms

We are going to modify only <build> sections of each pom. So If you extending your existing application with integration tests you’ll need to make just few adjustments.

  • Grails application pom (our demo-app). Go to <build> -> <plugins> section and insert jetty plugin:
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <stopKey>foo</stopKey>
                    <stopPort>9999</stopPort>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run-war</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

We’ll use it to launch our Grails application in the “integration-test” phase. And will click with Selenium on it, but a bit later about all that.

  1. Integration tests module. I’ll post here entire pom.xml because it’s not aware about project under test, only groupId could differ.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.grailsgeek</groupId>
  <artifactId>integration-test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>integration-test</name>
  <url>http://maven.apache.org</url>
  <dependencies>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy</artifactId>
            <version>1.6.3</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium.client-drivers</groupId>
            <artifactId>selenium-java-client-driver</artifactId>
            <version>1.0-beta-2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.groovy.maven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>generateTestStubs</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <fileset>
                                    <directory>${pom.basedir}/src/test</directory>
                                    <includes>
                                        <include>**/*.groovy</include>
                                    </includes>
                                </fileset>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>selenium-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start-server</goal>
                        </goals>
                        <configuration>
                            <background>true</background>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>surefire-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>seleniumhq</id>
            <name>seleniumhq</name>
            <url>http://nexus.openqa.org/content/repositories/releases/</url>
        </repository>
    </repositories>
</project>

I propose to run “mvn integration-test” from project root, and see what will happen now. There are no tests yet, but Maven should resolve all dependencies and pass all execution phases. “BUILD SUCCESSFUL” means that we could go on :)

Preparing simple functionality for our tests

Import our Maven project into Intellij IDEA as I described in my previous post.

Now I’m creating domain class Message:

public class Message {
    String text
}

And controller for it:

class MessageController {
    def scaffold = true
}

That’s it, our application is ready to be tested.

Writing first Selenium test

Switch to integration-test module, and create “integration-test/test/java/com/grailsgeek/MessageTest.groovy”:

package com.grailsgeek

import com.thoughtworks.selenium.SeleneseTestCase

public class MessageTest extends SeleneseTestCase {

    public void setUp() {
        setUp "http://localhost:8080/demo-app/", "*firefox"
    }

    void testCreateMessage() {
        selenium.with {
            speed = "3000"

            open "message"
            waitPage()
            assertEquals "Message List", title

            click "//a[text()=\"New Message\"]"
            waitPage()
            assertEquals "Create Message", title

            type "text","Message Text ;)"
            click "//input[@value=\"Create\"]"
            waitPage()
            assertEquals "Show Message", title

            assertTrue isTextPresent ("Message Text ;)")
        }
    }

    private def waitPage() {
        selenium.waitForPageToLoad "30000"
    }
}

Running tests from Maven

Run “mvn integration-test” from project root directory and enjoy :). But since your project is going to grow fast (I hope so) or you already big enough, you could experience problems with out of memory error (PermGen space) due to Groovy dynamism. You could easy solve this by adding “MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=256m” to your environment variables.

Running tests from Intellij IDEA

  1. Go to “Maven” context (at the right edge).
  2. Run your application “demo -> Modules -> demo-app -> Plugins -> grails -> grails:run-app” or just run IDEA grails configuration which should be already available.
  3. Run Selenium with “demo -> Modules -> integration-test -> Plugins -> selenium -> selenium:start-server”
  4. Execute your MessageTest.

Intellij IDEA allows you to save run configuration for ease of further use (selenium:start-server and grails:run-app could be saved as preconfigured). Also you there is no need to restart your application each time you made some changes. Grails will take care of it as usual.

Tags: , , , , , , , ,

Announce

Next I’ll try to describe how to write integration tests with Selenium on Grails driven by Surefire Maven plugin

Settin’ up Grails 1.1.1. with Grails Maven Plugin in Intellij IDEA + JetGroovy

Grails Maven Plugin is still non predictable and it’s 1.0 version supports only 1.1 version of Grails. 1.1-SNAPSHOT version supports grails 1.1.1 but there are still misunderstandings with Grails plugins are exist (Grails wants to store them in your user home, but  Grails Maven plugin wants them to be stored in your application dir).

As for now I’m trying to avoid using plugins from Grails, and include all libraries directly with Maven dependencies.

1. Make your maven know where to get grails-maven-plugin by editing your /.m2/settings.xml (create it if there is no one exist):

<settings>
    <pluginGroups>
        <pluginGroup>org.grails</pluginGroup>
    </pluginGroups>
</settings>

2. Go to secret place where you store you projects and run:

mvn org.apache.maven.plugins:maven-archetype-plugin:2.0-alpha-4:generate -DarchetypeGroupId=org.grails -DarchetypeArtifactId=grails-maven-archetype -DarchetypeVersion=1.0 -DgroupId=example -DartifactId=maven-grails-demo

3. Now open just generated pom.xml and make following changes:

  • change versions of grails-crud and grails-gorm from “1.1” to “1.1.1”
  • change grails-maven-plugin version from “1.0” to “1.1-SNAPSHOT”
4. Run “mvn initialize” from your project root.
5. One more trick, that should be applied for 1.1-SNAPSHOT version of grails-maven-plugin to make it work: add following dependency to your pom.xml (see http://jira.codehaus.org/browse/GRAILS-4574 for details):
<dependency>
    <groupId>org.tmatesoft.svnkit</groupId>
    <artifactId>svnkit</artifactId>
    <version>1.2.3.5521</version>
    <scope>runtime</scope>
</dependency>

6. Now, it’s time to import your maven project into Intellij IDEA (I recommend to use latest EAP http://www.jetbrains.net/confluence/display/IDEADEV/Maia+EAP). You can do it by “Create New Project → Import Project from External Model → Maven…”.

Pay attention to Facets that Idea will detect on project import finished. Hint: look in right bottom corner for round icon with “i” on it, if Idea won’t show you dialog window with detected facets. Facets detection is important to let Idea know that it’s Grails application.
Now close and reopen project again to let JetGroovy plugin use facets and treat application directories in a right way. You should see “Grails:maven-grails-demo” preselected in IDEA’s run/debug configurations combo. If it’s so – you on the right way and JetGroovy knows that it’s Grails application. If not – try to fix it in “module settings → facets”. If you didn’t make it – contact me, I’ll try to help.

You probably already tried to run application but all you’ve got was:
Exception in thread "main" java.lang.ClassNotFoundException: org.codehaus.groovy.grails.cli.support.GrailsStarter at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:87)
Don’t worry. It’s because grails-maven-plugin depends on grails-core which not contain GrailsStarter. It’s available in standalone Grails distribution. First of all you are still able to run grails application with “Maven Projects → maven-grails-demo → Plugins → grails → grails:run-app” or just by running “mvn grails:run-app” from project root. If you still want to run your application from run configuration in Idea you could or create run configuration from Maven goal, by clicking on with right button and selecting appropriate menu item or by installing standalone Grails and telling IDEA where it is.

Setting up standalone Grails for your application

  • Install Grails as described http://grails.org/Installation.
  • Go to module settings → Groovy → click “Add…” → Create new Grails SDK… → Tell Idea where your standalone Grails is → Click “Replace” (not “Add”, important) in dialog appeared
Now you able to run your Grails application directly from Idea with JetGroovy plugin. Enjoy ;)

Tags: , , , , ,

Greetings!

Hi, dear Community! I’ve just get back into Grails experiments after few months time-out. I’m going to share with you all essential solutions that I found already or just going to.