我正在使用Grails 3.1.9而我正在尝试了解如何测试服务。这是我的实体:
class Flight {
String number
Plane plane
Airport origin
Airport destination
LocalDate departureDate
LocalTime departureTime
LocalDate arrivalDate
LocalTime arrivalTime
BigDecimal ticketPrice
boolean cancelled
static hasMany = [
bookings: Booking
]
static constraints = {
}
}
这是我的服务:
@Transactional
class FlightService {
List<Flight> search(Airport from, Airport to, LocalDate on) {
return Flight.findAllByOriginAndDestinationAndDepartureDate(from, to, on)
}
}
这是我的测试:
@TestFor(FlightService)
@Mock([Flight])
class FlightServiceSpec extends Specification {
def setup() {
}
def cleanup() {
}
void "test search"() {
when:
def sfo = new Airport(code: 'SFO', city: 'San Francisco', countryCode: 'US', timeZone: ZoneId.of("America/Los_Angeles"))
def bru = new Airport(code: 'BRU', city: 'Brussels', countryCode: 'BE', timeZone: ZoneId.of("Europe/Brussels"))
service.search(bru, sfo, LocalDate.of(2016, 8, 31))
then:
1 * Flight.findAllByOriginAndDestinationAndDepartureDate(*_)
}
}
但是当我运行grails test-app时,我在报告中得到以下异常:
org.springframework.dao.InvalidDataAccessResourceUsageException: Cannot query [com.epseelon.atr.Flight] on non-existent property: departureDate
at org.grails.datastore.mapping.simple.query.SimpleMapQuery.getValidProperty(SimpleMapQuery.groovy:751)
at org.grails.datastore.mapping.simple.query.SimpleMapQuery.executeSubQueryInternal(SimpleMapQuery.groovy:690)
at org.grails.datastore.mapping.simple.query.SimpleMapQuery.executeSubQuery(SimpleMapQuery.groovy:676)
at org.grails.datastore.mapping.simple.query.SimpleMapQuery.executeQuery(SimpleMapQuery.groovy:63)
at org.grails.datastore.mapping.query.Query.list(Query.java:567)
at org.grails.datastore.gorm.finders.FindAllByFinder.invokeQuery(FindAllByFinder.java:49)
at org.grails.datastore.gorm.finders.FindAllByFinder$1.doInSession(FindAllByFinder.java:43)
at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:310)
at org.grails.datastore.gorm.finders.AbstractFinder.execute(AbstractFinder.java:41)
at org.grails.datastore.gorm.finders.FindAllByFinder.doInvokeInternal(FindAllByFinder.java:40)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:157)
at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:357)
at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:141)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.staticMethodMissing(GormEntity.groovy:745)
at com.epseelon.atr.FlightService.$tt__search(FlightService.groovy:11)
at com.epseelon.atr.FlightService.search_closure1(FlightService.groovy)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
at com.epseelon.atr.FlightServiceSpec.test search(FlightServiceSpec.groovy:27)
我不明白,因为Flight实体上有一个departureDate属性。
答案 0 :(得分:0)
将其添加到您的规范中以启用joda时间单元测试支持
def setupSpec() {
SimpleMapJodaTimeMarshaller.initialize();
}