1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import org.grails.shortenurl.* class ShortenUrlService { boolean transactional = false def tinyurl(String longUrl) { return TinyUrl.shorten(longUrl) } def isgd(String longUrl) { def shortUrl = IsGd.shorten(longUrl) if(shortUrl.contains("error")){ log.error(shortUrl) } return shortUrl } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import grails.test.* class ShortenUrlServiceTests extends GrailsUnitTestCase { def transactional = false def shortenUrlService protected void setUp() { super.setUp() shortenUrlService = new ShortenUrlService() } protected void tearDown() { super.tearDown() } void testTinyUrl() { def shortUrl = shortenUrlService.tinyurl("http://grails.org") assertEquals "http://tinyurl.com/3xfpkv", shortUrl } void testIsGd() { def shortUrl = shortenUrlService.isgd("http://grails.org") assertEquals "http://is.gd/2oCZR", shortUrl } void testIsGdWithBadUrl() { def shortUrl = shortenUrlService.isgd("IAmNotAValidUrl") assertTrue shortUrl.startsWith("An error occurred:") } } |
1 2 3 4 5 | protected void setUp() { super.setUp() mockLogging(ShortenUrlService) shortenUrlService = new ShortenUrlService() } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |