grails how to create a unit test
Solution 1
65.000 for 2.4 kernels, and 4 billion for 2.6 kernels.
Solution 2
Here you go:
grails 2.3.x uses spock framework by default for testing
//Controller class
class DomainListController {
def index() {
redirect (action:"list")
}
def list() {
render "hello"
}
}
//Test class
@TestFor(DomainListController) class DomainListControllerSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test something"() {
}
//test method to test index() of DomainListController
def void testIndex() {
controller.index()
expect:
response.redirectedUrl == 'domainList/listll'
}
//test method to test list() of DomainListController
def void testList() {
controller.list()
expect:
response.text == "hello"
}
}
--> @TestFor mixin takes care of the contoller mocking. You can very well access some keywords here like controller, contoller.params, controller.request, controller.response without instantiating controller.
-->The response object is an instance of GrailsMockHttpServletResponse (from the package org.codehaus.groovy.grails.plugins.testing) which extends Spring's MockHttpServletResponse class and has a number of useful methods for inspecting the state of the response.
-->expect: is the expected result
Hope this helps you :) cheers - Mothi
Solution 3
i managed to fix the issue by changing how the test is written eg
void "test Something"() {
controller.index()
expect:
"Welcome to the gTunes store!" == response.text
}
this is because grails now uses the spock test framework by default
Related videos on Youtube
user2168435
Updated on November 25, 2022Comments
-
user2168435 over 1 year
I am a bit new to using grails so please forgive me if this has an obvious answer. I have been going through the documentation on how unit tests are automatically created when you create a new controller. from what i have seen online and in books, the controller test class name is appended with "test" at the end. using grails 2.3.1 in the \test\unit\ directory it created StoreControllerSpec.groovy in that i have
@TestFor(StoreController) class StoreControllerSpec extends Specification { def setup() { } def cleanup() { } void testSomething() { \\ added to see if the test works controller.index() assert 'Welcome to the gTunes store!' == response.text } }
the problem I am having is that when running test-app it tries to run the unit test but outputs nothing and it is not marked as failed?
grails> test-app | Running without daemon... | Compiling 1 source files | Compiling 1 source files. | Running 1 unit test... | Completed 0 unit test, 0 failed in 0m 4s | Tests PASSED - view reports in
-
Admin over 13 yearsWhen are they gonna fix that? What if I want to make one account for every person in the world?
-
Admin over 13 yearsWell, doesn't 64-bit linux support 2^128 users?
-
Admin over 13 yearsFun fact: assuming an the average entry in /etc/passwd is 75 bytes, 4 billion users would give you an /etc/passwd that's just a shade under 300GB. :-)
-
Admin over 13 years@shintoist: Because things are made based on what's probable, not what's possible. It's possible that you would want to create a user account in a single Linux system for every person in the world, but it's not probable that you'd actually do that.
-
Admin over 13 years@joeqwert Yes, because I was being serious...
-
Admin over 13 yearsDo you have any source of these numbers?
-
Admin over 13 years@Graeme. Haha. Of course you don't necessarily need to use
passwd
andnsswitch compat
. -
user2168435 over 10 yearssorry no luck, i renamed the class and it did the same thing
-
Burt Beckwith over 10 yearsRight - Spock test methods require at least one labelled block, see code.google.com/p/spock/wiki/SpockBasics
-
Admin almost 2 yearsDon’t you mean 6,5536 (2¹⁶) or 4,294,967,296 (2³²)? (When including root (0) and everything below 1000.) 65000 is a very unnatural number for computers.
-
Admin almost 2 years@ThatGraemeGuy: Usually, use uses things like LDAP/Kerberos for these situations. SQL-database-backed probably.