Я пытаюсь получить MongoTemplate
(Spring Data), работающий с моим тестом спецификации Спока. Я использую Kotlin в качестве языка для бизнес-логики.
Пожалуйста, ознакомьтесь с моей спецификацией логики:
@SpringBootTest
class BookingServiceSpec extends Specification {
BookingService bookingService;
BookingEntity bookingEntity
CustomerEntity customerEntity
MongoTemplate mongoTemplate;
def setup() {
bookingEntity = GroovyMock(BookingEntity)
customerEntity = GroovyMock(CustomerEntity)
mongoTemplate = Mock()
bookingService = new BookingService(mongoTemplate)
customerEntity.getEmail() >> "test@test.com"
mongoTemplate.find(!null, !null, !null) >> List.of(bookingEntity)
}
def "should return a list of bookings if asked for bookings for a customer"() {
when: "booking service is used to find bookings for a given customer"
List<BookingEntity> bookings = bookingService.getBookings(customerEntity)
then: "it should call the find method of the mongo template and return a list of booking entities"
1 * mongoTemplate.find(!null, !null, !null)
}
}
При запуске этого кода выдается IllegalStateException
с подробной информацией:
java.lang.IllegalStateException: mongoTemplate.find(query…::class.java, "bookings") must not be null
at com.nomadsolutions.areavr.booking.BookingService.getBookings(BookingService.kt:22)
at com.nomadsolutions.areavr.booking.BookingServiceSpec.should return a list of bookings if asked for bookings for a customer(BookingServiceSpec.groovy:37)
Классы данных сущности определены следующим образом:
data class CustomerEntity(val surname: String, val name: String, val email: String)
data class BookingEntity(val customerEntity: CustomerEntity, val date: Date)
И вот бизнес-логика:
@Service
class BookingService(var mongoTemplate: MongoTemplate) {
fun addBooking(booking: BookingEntity) {
mongoTemplate.insert(booking)
}
fun getBookings(customerEntity: CustomerEntity): List<BookingEntity> {
val query = Query()
query.addCriteria(Criteria.where("customer.email").`is`(customerEntity.email))
return mongoTemplate.find(query, BookingEntity::class.java, "bookings")
}
}
Я обнаружил, что заглушка для customerEntity
не работает должным образом, так какcustomerEntity.email
возвращает null
в логике при отладке с помощью тестового прогона.
Я бы хотел продолжить работу со Споком, но мне кажется, что это мешает мне проводить быстрое тестирование, поскольку мне нужно заботиться о таких вещах.