Grails testcase失败

时间:2012-06-20 12:23:33

标签: grails groovy junit grails-2.0

我有像

这样的域类
package trip.side
import java.text.SimpleDateFormat

class HotelStay {
    String hotel
    Date checkIn
    Date checkOut

    static constraints = {
    }
    String toString(){
     def sdf = new SimpleDateFormat("EEEE")
    "${hotel}(${sdf.format(checkIn)} to ${sdf.format(checkOut)})"
    }

}

并在HotelStayTests

中写了一个测试用例toString方法
void testToString() {
      def h = new HotelStay(hotel:"Hilton")
      def df = new SimpleDateFormat("MM/dd/yyyy")
      h.checkIn = df.parse("10/1/2008")
      h.checkOut = df.parse("10/5/2008")
      println h
      assertToString h, "Hilton (Wednesday to Sunday)"
    }

完成HotelStayTests课程

package trip.side



import grails.test.mixin.*
import org.junit.*
import java.text.SimpleDateFormat

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(HotelStay)
class HotelStayTests {

   void testSomething() {
   // Simple test by creating new object and asserting it
          // fail "Implement me"
        HotelStay hs = new HotelStay(hotel:"Ibis")
        assertEquals "Ibis", hs.hotel
    }   

    void testToString() {
      def h = new HotelStay(hotel:"Hilton")
      def df = new SimpleDateFormat("MM/dd/yyyy")
      h.checkIn = df.parse("10/1/2008")
      h.checkOut = df.parse("10/5/2008")
      println h
      assertToString h, "Hilton (Wednesday to Sunday)"
    }
}

但失败并提供错误报告

No signature of method: trip.side.HotelStayTests.assertToString() is applicable for argument types: (trip.side.HotelStay, java.lang.String) values: [Hilton(Wednesday to Sunday), Hilton (Wednesday to Sunday)] Possible solutions: testToString()
groovy.lang.MissingMethodException: No signature of method: trip.side.HotelStayTests.assertToString() is applicable for argument types: (trip.side.HotelStay, java.lang.String) values: [Hilton(Wednesday to Sunday), Hilton (Wednesday to Sunday)]
Possible solutions: testToString()
    at trip.side.HotelStayTests.testToString(HotelStayTests.groovy:28)
System output
Hilton(Wednesday to Sunday)

知道这里出了什么问题吗?

1 个答案:

答案 0 :(得分:3)

assertToStringthe GroovyTestCase class的一部分。

您的测试类需要extend GroovyTestCase才能获得此功能