我是一个 grails/groovy 新手,所以请原谅我这个简单的问题,但我真的尝试了一切,但我没有成功。一般来说,我想为我的服务方法编写非常简单的单元测试。但我无法弄清楚为什么我的模拟没有列表。
我的服务:
class MenuService {
def mailService
def prepareMenu() {
def newDinners = []
def soups = Dish.findAllWhere(dishKind: DishKind.SOUP)
def mainCourses = Dish.findAllWhere(dishKind: DishKind.MAIN_COURSE)
def salads = Dish.findAllWhere(dishKind: DishKind.SALAD)
def menu = checkIfMenuExists(Menu.last())
deleteUsedMainCoursesAndSoups(menu, soups, mainCourses)
deleteUsedSalads(menu, salads)
Collections.shuffle(mainCourses)
mainCourses = mainCourses.subList(0, 7);
mainCourses.each {
Dinner dinner = new Dinner()
dinner.mainCourse = it
if (it.withSoup) {
Collections.shuffle(soups)
dinner.soup = soups.get(0)
soups.remove(0)
}
newDinners.add(dinner)
}
Collections.shuffle(salads)
salads = salads.subList(0, 2)
menu.dinners = newDinners
menu.salads = salads
menu.save()
sendMenu(menu)
}
private void deleteUsedSalads(Menu menu, salads) {
if (!menu.salads.isEmpty()) {
menu.salads.each {
if (salads.contains(it)) {
salads.remove(it)
}
}
}
}
private void deleteUsedMainCoursesAndSoups(Menu menu, soups, mainCourses) {
if (!menu.dinners.isEmpty()) {
menu.dinners.each {
if (mainCourses.contains(it.mainCourse)) {
mainCourses.remove(it)
} else if (soups.contains(it.soup))
soups.remove(it)
}
}
}
private Menu checkIfMenuExists(Menu menu) {
if (menu == null) {
menu = new Menu()
menu.dinners = new ArrayList<Dinner>()
menu.salads = new ArrayList<Dish>()
}
menu
}
def sendMenu(Menu menu) {
mailService.sendMail {
to ""
subject ""
body(view: "/mail", model: [dinners: menu.dinners, salads: menu.salads])
}
}
}
我有一个测试:
@TestFor(MenuService)
@Mock([Dish, Menu])
@TestMixin(DomainClassUnitTestMixin)
class MenuServiceTests {
def menuService = new MenuService()
void testIfItChoosesNewMenu() {
def soups = [
new Dish(name: "Soup 1", dishKind: DishKind.SOUP, ingredients: ""),
new Dish(name: "Soup 2", dishKind: DishKind.SOUP, ingredients: ""),
new Dish(name: "Soup 3", dishKind: DishKind.SOUP, ingredients: ""),
new Dish(name: "Soup 4", dishKind: DishKind.SOUP, ingredients: ""),
new Dish(name: "Soup 5", dishKind: DishKind.SOUP, ingredients: ""),
new Dish(name: "Soup 6", dishKind: DishKind.SOUP, ingredients: ""),
new Dish(name: "Soup 7", dishKind: DishKind.SOUP, ingredients: "")
]
def mainCourses = [
new Dish(name: "Main course 1", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 2", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 3", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 4", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 5", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 6", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 7", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 8", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 9", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 10", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 11", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 12", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 13", dishKind: DishKind.MAIN_COURSE, ingredients: ""),
new Dish(name: "Main course 14", dishKind: DishKind.MAIN_COURSE, ingredients: "")
]
def salads = [
new Dish(name: "Salad 1", dishKind: DishKind.SALAD, ingredients: ""),
new Dish(name: "Salad 2", dishKind: DishKind.SALAD, ingredients: ""),
new Dish(name: "Salad 3", dishKind: DishKind.SALAD, ingredients: ""),
new Dish(name: "Salad 4", dishKind: DishKind.SALAD, ingredients: ""),
]
mockDomain(Dish, [soups, mainCourses, salads])
Menu menu = menuService.prepareMenu()
assertEquals 2, menu.salads.size()
assertEquals 7, menu.dinners.size()
}
}
当我运行测试时,我失败了:
Failure: testIfItChoosesNewMenu(familyhelper.MenuServiceTests)
java.lang.IndexOutOfBoundsException: toIndex = 7
at java.util.SubList.<init>(AbstractList.java:602)
at java.util.RandomAccessSubList.<init>(AbstractList.java:758)
at java.util.AbstractList.subList(AbstractList.java:468)
at familyhelper.MenuService.prepareMenu(MenuService.groovy:18)
at familyhelper.MenuServiceTests.testIfItChoosesNewMenu(MenuServiceTests.groovy:51)
因为:
def soups = Dish.findAllWhere(dishKind: DishKind.SOUP)
def mainCourses = Dish.findAllWhere(dishKind: DishKind.MAIN_COURSE)
def salads = Dish.findAllWhere(dishKind: DishKind.SALAD)
是空的。我已经读过,当我添加 @Mock 注释时,域对象的所有动态函数都会被注入,但即使这样也无济于事。有人知道如何解决这个测试吗?提前致谢