Grails测试用例失败
我有一个域类,比如Grails测试用例失败,grails,groovy,junit,grails-2.0,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}(${
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)
知道这里出了什么问题吗?
assertToString
是其中的一部分
您的测试类需要
扩展GroovyTestCase
才能获得此功能您的测试类是否扩展GroovyTestCase
?@tim_-yates:否。。我已经编辑了我的问题assertToString
是GroovyTestCase
的一部分。。。您是否尝试过将扩展GroovyTestCase
放入类定义中?以何种方式不起作用?同样的例外情况?这很有效。。。您希望希尔顿Hilton
和(
)之间有一个空格,但在您的toString
方法中,没有空格……如果我不扩展GroovyTestCase(如grails 2.0)并想要实现相同的功能,该怎么办?我的意思是assertToString的替代方案是什么?您可以这样做:assert h?.toString()='Hilton(星期三至星期日)