GrailsInAction / graina2

Source code for the 2nd edition of Grails in Action
90 stars 92 forks source link

MEAP ch1 v12, Issues with *Spec.groovy vs. *Tests.groovy #55

Closed tikeswar closed 10 years ago

tikeswar commented 10 years ago

When I run grails create-*, instead of generating *Tests.groovy (as mentioned in the book), it generates *Spec.groovy. Below are the examples.

Example 1: Pg. 10 grails create-controller quote creates | Created file test/unit/qotd/QuoteControllerSpec.groovy instead of (as mentioned in the book) | Created file test/unit/qotd/QuoteControllerTests.groovy

Example 2: Pg. 17 grails create-domain-class quote creates | Created file test/unit/qotd/QuoteSpec.groovy instead of (as mentioned in the book) | Created file test/unit/qotd/QuoteTests.groovy

Example 3: Pg. 25 grails create-service quote creates | Created file test/unit/qotd/QuoteServiceSpec.groovy instead of | Created file test/unit/qotd/QuoteServiceTests.groovy

Anyways, I added a method called testStaticQuoteReturnsQuicheQuote in my QuoteServiceSpec.groovy class as follows, and then ran grails test-app QuoteServiceSpec and it did NOT run any tests (in the command prompt I got | Completed 0 unit test, 0 failed in 0m 1s).

Here is my QuoteServiceSpec.groovy class.

package qotd

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(QuoteService)
class QuoteServiceSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
    }

    void testStaticQuoteReturnsQuicheQuote(){
        Quote staticQuote = service.getStaticQuote()
        assertEquals("Anonymous", staticQuote.author)
        assertEquals("Real Programmers Don't eat quiche", 
        staticQuote.content)
    }
}

My questions:

  1. Is the Spec.groovy vs. Tests.groovy issue that I am seeing because of the version I am using (Grails version: 2.3.1, Groovy version: 2.1.8, JVM version: 1.7.0_45), compared to the version the chapter was written for?
  2. How to run the test for the quote service successfully in Grails version 2.3.1?

Thank you in advance for any suggestions.

pledbrook commented 10 years ago
  1. Yes, Grails 2.3 now defaults to creating Spock tests
  2. I'll need to look into this. I could be the test method name or something else.

BTW, you will also need to remove the Spock plugin from the dependencies in grails-app/conf/BuildConfig.groovy if you are using Grails 2.3. Spock is included in Grails itself now.

tikeswar commented 10 years ago

Thank you again for the quick feedback, Peter. I think I managed to dig a solution from the internet.

Instead of using assertEquals, I used expect: and and:, and it seems to work fine now. Meaning, now if I run grails test-app QuoteServiceSpec, in the command prompt I get | Completed 1 unit test, 0 failed in 0m 1s. Note that earlier I was getting | Completed 0 unit test, 0 failed in 0m 1s. I also verified by opening /qotd/target/test-reports/html/index.html in a web browser and it says "A single test executed without a single error or failure!".

I am including my modified QuoteServiceSpec.groovy class for reference. Note the only changes made in the testStaticQuoteReturnsQuicheQuote method, and everything else the same.

package qotd
import grails.test.mixin.TestFor
import spock.lang.Specification
/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(QuoteService)
class QuoteServiceSpec extends Specification {
    def setup() {
    }
    def cleanup() {
    }
    void "test something"() {
    }

    void testStaticQuoteReturnsQuicheQuote(){
        Quote staticQuote = service.getStaticQuote()
        //assertEquals("Anonymous", staticQuote.author)
        //assertEquals("Real Programmers Don't eat quiche", 
        //staticQuote.content)
        expect:
            staticQuote.author == "Anonymous"
        and:
            staticQuote.content == "Real Programmers Don't eat quiche"
    }
}

Sorry, I am not experienced enough yet to explain why "assertEquals" does not work and "expect:" works, but here are a few reference I Googled that were helpful ... http://stackoverflow.com/questions/18999343/how-do-you-test-service-or-controller-methods-in-grails-2-3 https://code.google.com/p/spock/wiki/SpockBasics http://jira.grails.org/browse/GRAILS-10538

Also, on a side note to "BTW, you will also need to remove the Spock plugin from the dependencies in grails-app/conf/BuildConfig.groovy if you are using Grails 2.3. Spock is included in Grails itself now.". In my case the BuildConfig.groovy was automatically generated by Grails and the Spoke plugin was not included to start with (therefore I did not have to remove the plugin). Thank you!

pledbrook commented 10 years ago

Chapter has now been updated to use Spock tests, with a mention that Grails 2.2 and earlier automatically create JUnit tests.