模拟域对象不起作用

时间:2013-11-12 23:30:17

标签: unit-testing grails mocking

我是一个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注释时,会注入域对象的所有动态函数,但即使这对我没有帮助。有人知道如何修复这个测试吗?提前致谢

3 个答案:

答案 0 :(得分: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()
    }
} 

如果您还没有认识到build test data插件,那么我建议将其用于这类测试。总的来说,我还建议使用spock作为测试框架。

答案 1 :(得分: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(),
    ]

答案 2 :(得分:1)

要使用GORM在数据库中查找内容,您需要先保存。 @Alidad是对的,但你应该使用:

 new Dish(...).save(false)
false中的

save 会禁用您的域实体验证。