Discussion:
unit testing grails controller using redirect with mapping doesn't work
Kevin Richards
2013-02-12 11:46:17 UTC
Permalink
Hi,

We have some controllers that redirect using the UrlMappings named mappings
feature e.g.

redirect mapping: 'dbgConfirm', params:[gameName: command.gameName]

When unit testing the response.redirectUrl is not returning the mapped
value, it just returns the default controller action. How can we
initialise or mock the UrlMappings named mappings.

Thanks

Kevin Richards
Energized Work

email: ***@energizedwork.com <***@avoco.eu>
mobile: 07776304005
http://www.linkedin.com/in/grailsdeveloper

After 12 agile years has anything really changed?
Read ‘No Bull’ by @energizedwork at http://nobull.energizedwork.com (PDF)
Dean Del Ponte
2013-02-12 15:44:22 UTC
Permalink
What if you try mocking out your UrlMappings as described here:
http://grails.org/doc/latest/guide/testing.html#unitTestingURLMappings

Simulating Controller Mapping

In addition to the assertions to check the validity of URL mappings you can
also simulate mapping to a controller by using your UrlMappings as a mock
collaborator and the mapURI method. For example:

@TestFor(SimpleController)
@Mock(UrlMappings)
class SimpleControllerTests {

void testControllerMapping() {

SimpleController controller = mapURI('/simple/list')
assert controller != null

def model = controller.list()
assert model != null
}
}
Post by Kevin Richards
Hi,
We have some controllers that redirect using the UrlMappings named
mappings feature e.g.
redirect mapping: 'dbgConfirm', params:[gameName: command.gameName]
When unit testing the response.redirectUrl is not returning the mapped
value, it just returns the default controller action. How can we
initialise or mock the UrlMappings named mappings.
Thanks
Kevin Richards
Energized Work
mobile: 07776304005
http://www.linkedin.com/in/grailsdeveloper
After 12 agile years has anything really changed?
Kevin Richards
2013-03-01 19:09:43 UTC
Permalink
Thanks I got it to work but I had a slight problem. When it came to running
the test I could not find UrlMappings (and IntelliJ wasn't happy either
even though I added the conf folder to the source folders).

In the end I had to create a new UrlMappings file in a package,
(web.UrlMappings) and then everything was fine. Is this normal???

Kev
Post by Dean Del Ponte
http://grails.org/doc/latest/guide/testing.html#unitTestingURLMappings
Simulating Controller Mapping
In addition to the assertions to check the validity of URL mappings you
can also simulate mapping to a controller by using your UrlMappings as a
@TestFor(SimpleController)
@Mock(UrlMappings)
class SimpleControllerTests {
void testControllerMapping() {
SimpleController controller = mapURI('/simple/list')
assert controller != null
def model = controller.list()
assert model != null
}
}
Post by Kevin Richards
Hi,
We have some controllers that redirect using the UrlMappings named
mappings feature e.g.
redirect mapping: 'dbgConfirm', params:[gameName: command.gameName]
When unit testing the response.redirectUrl is not returning the mapped
value, it just returns the default controller action. How can we
initialise or mock the UrlMappings named mappings.
Thanks
Kevin Richards
Energized Work
mobile: 07776304005
http://www.linkedin.com/in/grailsdeveloper
After 12 agile years has anything really changed?
Dean Del Ponte
2013-03-05 17:01:38 UTC
Permalink
Doesn't sound normal :)

But I'm not sure what's causing it in your case.

Dean Del Ponte
Post by Kevin Richards
Thanks I got it to work but I had a slight problem. When it came to
running the test I could not find UrlMappings (and IntelliJ wasn't happy
either even though I added the conf folder to the source folders).
In the end I had to create a new UrlMappings file in a package,
(web.UrlMappings) and then everything was fine. Is this normal???
Kev
Post by Dean Del Ponte
http://grails.org/doc/latest/guide/testing.html#unitTestingURLMappings
Simulating Controller Mapping
In addition to the assertions to check the validity of URL mappings you
can also simulate mapping to a controller by using your UrlMappings as a
@TestFor(SimpleController)
@Mock(UrlMappings)
class SimpleControllerTests {
void testControllerMapping() {
SimpleController controller = mapURI('/simple/list')
assert controller != null
def model = controller.list()
assert model != null
}
}
Post by Kevin Richards
Hi,
We have some controllers that redirect using the UrlMappings named
mappings feature e.g.
redirect mapping: 'dbgConfirm', params:[gameName: command.gameName]
When unit testing the response.redirectUrl is not returning the mapped
value, it just returns the default controller action. How can we
initialise or mock the UrlMappings named mappings.
Thanks
Kevin Richards
Energized Work
mobile: 07776304005
http://www.linkedin.com/in/grailsdeveloper
After 12 agile years has anything really changed?
Loading...