grails 2.3: tests not recognized by test-app - grails

I am playing with the recently released grails 2.3.0. Unfortunately, test-app is not recognizing tests. Here is what I did to produce the problem.
First, make a new app and create a controller:
$ grails create-app firstApp
$ cd firstApp/
$ grails create-controller foo
I got the following files from creating the controller foo:
| Created file grails-app/controllers/firstapp/FooController.groovy
| Created file grails-app/views/foo
| Created file test/unit/firstapp/FooControllerSpec.groovy
Then, I edited the file FooControllerSpec.groovy by adding assert 1 == 2 in the auto-generated method void "test something"(). Here is the full content of FooControllerSpec.groovy after my editing:
package firstapp
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
* See the API for {#link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
*/
#TestFor(FooController)
class FooControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test something"() {
assert 1 == 2
}
}
Then I ran the following command:
$ grails test-app
However, no tests were run. Here is the output from that command:
| Completed 0 unit test, 0 failed in 0m 0s
| Tests PASSED - view reports in /Users/jianbao.tao/projects/grails/firstApp/target/test-reports
My platform is OS X 10.8.5 + grails 2.3.0 + Java 1.6.0_51 + groovy 2.1.6. Can anyone tell me what's going on here, please? Thank you in advance.

Grails 2.3.0 ships with spock test framework by default. So, the test should look like:
void "test something"() {
expect:
1 == 2
}
For details on spockframework, visit docs.

Related

Wrong coverage report with WebTest

I have a pyramid application that I am testing using WebTest with the following code:
class FunctionalTests(unittest.TestCase):
def setUp(self):
from test_app import main
app = main({})
from webtest import TestApp
self.testapp = TestApp(app)
def test_not_found(self):
self.testapp.get("/not_found", status=404)
The view for not found is defined as follow:
#notfound_view_config(renderer="../templates/404.jinja2")
def notfound_view(request):
request.response.status = 404 # This is line 6
return {} # This is line 7
The coverage for the no found view is:
Name Stmts Miss Branch BrPart Cover Missing
test_app/views/notfound.py 4 2 0 0 50% 6-7
But there is no way that those line were skipped in the test so why coverage missed them?
This is my .coveragerc
[run]
branch = True
source = test_app
# omit = bad_file.py
[paths]
source =
test_app/
*/site-packages/
[report]
show_missing = True
I run the test with:
pytest --cov=formshare

Rendering a gsp not working from controller grails

I am facing an issue that i am not able to render a gsp view from a controller.
I did following :
1) create-app demo
2) grails ( open grails cmd)
3) create-controller check.work
package check
class WorkController {
def index() { render(view:'foo') }
}
4) put some content in grails-app/views/work/foo.gsp
5) started grails run-app on 8080
Issue is that if i just render("hello"), it works and prints on browser. But loading gsp file doesn't print any thing and return with 200.
URL used : http://localhost:8080/work/index
Have anyone faced such issue before ?
Grails Version: 3.1.11
| Groovy Version: 2.4.7
| JVM Version: 1.8.0_111

What classspath is used for executing Grails' application.groovy

What classspath is used for compiling/executing Grails' application.groovy?
In my application.groovy, I instantiate a custom class (contained in a dependency's jar) and assign it to one of the config properties, like so:
environments {
production {
configProperty = new com.example.CustomClass()
I recently upgraded my application from Grails 3.1.5 to 3.2.2, and now this no longer works.
I receive an error like the following when I try to run grails run-app:
Error occurred running Grails CLI: startup failed:
script14788250424471597489853.groovy: 43: unable to resolve class com.example.CustomClass
# line 43, column 33.
configProperty = new com.example.CustomClass()
(Notice that the code is in the production block, but I'm running in development (run-app). That makes me think it's the compilation of this script that is failing.)
So I'm guessing I just need to add my dependency (that contains the CustomClass) to the appropriate classpath, but I'm not sure which one.
I'm using gradle, and have the following in my build.gradle file, to pull in the dependency containing CustomClass:
buildscript {
dependencies {
classpath "com.example:custom-module:1.1"
// ...
dependencies {
compile group: 'com.example', name: 'custom-module', version:'1.1'
}
The grails-app/conf/application.groovy file shouldn't reference application classes because it is read before compilation. If you wish to reference application classes in configuration please use grails-app/conf/runtime.groovy

Grails defaultPackage ignored?

When I use create-controller on the command line or in IntelliJ, defaultPackage is ignored. Instead, the controller (or service, etc.) is placed in '#artifact.package.path#':
| Enter a command name to run. Use TAB for completion:
grails> create-controller ThingController
| Created grails-app/controllers/#artifact.package.path#/ThingController.groovy
| Created src/test/groovy/#artifact.package.path#/ThingControllerSpec.groovy
I'm not sure if this is a bug in my code or Grails, perhaps because I moved config from application.yml to application.groovy:
grails{
profile = 'web'
codegen{
defaultPackage = 'com.madeupname.web'
}
spring {
transactionManagement {
proxies = false
}
}
}
Maybe there's a typo I'm blind to? I'm using Grails 3.1.7 on JDK 8.
This will work if you define the property in grails-app/conf/application.yml.
---
grails:
codegen:
defaultPackage: com.madeupname.web

doWithDynamicMethods not being Called

When I create a production war (grails war --nojars) my doWithDynamicMethods is not being called in a production environment. The code works in the dev env (ie grails run-app)
Here is some of my code:
PDFFormsGrailsPlugin.groovy (in PDFForms (plugin) directory) :-
def doWithDynamicMethods = { ctx ->
println "Adding renderPDFForm to controller";
for (controllerClass in application.controllerClasses) {
updateControllers controllerClass.metaClass
}
}
BuildConfig.groovy (in Application using Plugin) :-
grails.plugin.location.'pdff-orms' = "../PDFForms"
We are using Grails 1.3.5 on Windows XP, The Build is called from STS 2.3.3.CI-R5462-B20
Thanks in advance.
Check if the plugin is referenced in your grails.xml.
Read this Nabble thread
And this jira
P.S. Why not you upgrade to grails 1.3.7 at least?!

Resources