0

我是一个 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 注释时,域对象的所有动态函数都会被注入,但即使这样也无济于事。有人知道如何解决这个测试吗?提前致谢

4

3 回答 3

2

您不需要显式模拟域类。@Mock 应该为您做到这一点。

@TestFor(MenuService)
@Mock([Dish, Menu])
class MenuServiceTests {

    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: "")
        ]

        //Save the domain classes
        [soups, mainCourses, salads].each{it*.save(flush: true, failOnError: true)}

        //Note service is auto injected by @TestFor
        //SO no need to instantiate
        Menu menu = service.prepareMenu() 

        assertEquals 2, menu.salads.size()
        assertEquals 7, menu.dinners.size()
    }
} 

如果您还不了解构建测试数据插件,那么我建议您将其用于此类测试。总的来说,我还建议使用 spock 作为测试框架。

于 2013-11-13T00:16:15.150 回答
1

要使用 GORM 在 DB 中查找某些内容,您需要先保存它。@Alidad 是对的,但您可能应该使用:

 new Dish(...).save(false)

falseinsave禁用您的域实体验证。

于 2013-11-13T08:08:03.753 回答
1

可能你忘了保存盘子,

def salads = [
            new Dish(name: "Salad 1", dishKind: DishKind.SALAD, ingredients: "").save(),
            new Dish(name: "Salad 2", dishKind: DishKind.SALAD, ingredients: "").save(),
            new Dish(name: "Salad 3", dishKind: DishKind.SALAD, ingredients: "").save(),
            new Dish(name: "Salad 4", dishKind: DishKind.SALAD, ingredients: "").save(),
    ]
于 2013-11-13T00:15:51.877 回答