2011-11-19 93 views
1

我是grails的新手,正在尝试运行一些测试。我有两个类:“junit.framework.AssertionFailedError:null”在Grails单元测试中

class Category { 
    String name; 
} 

class Animal { 
    Category category1 
    Category category2 

    boolean addCategory(Category category) { 
     if (!category1) { 
      category1 = category 
      return true 
     } 
     else if (!category2) { 
      category2 = category 
      return true 
     } 
     return false 
    } 

    boolean hasCategory(Category category) { 
     return category1 == category || category2 == category 
    } 
} 

现在,我写了一个测试,应检查,添加两个以上类别的失败,第三类就不会是类的一部分:总是

class AnimalTests extends GrailsUnitTestCase { 
    protected void setUp() { 
     super.setUp() 
    } 

    protected void tearDown() { 
     super.tearDown() 
    } 

    void testMaximumCategories() { 
     Category category1 = new Category(name: "Category 1") 
     Category category2 = new Category(name: "Category 2") 
     Category category3 = new Category(name: "Category 3") 

     Animal animal = new Animal(title: "Animal") 
     assertTrue(animal.addCategory(category1)) 
     assertTrue(animal.addCategory(category2)) 
     assertFalse(animal.addCategory(category3)) 

     assertTrue(animal.hasCategory(category1)) 
     assertTrue(animal.hasCategory(category2)) 
     assertFalse(animal.hasCategory(category3)) 
    } 
} 

这个测试未能在最后一行和堆栈跟踪如下

null 
junit.framework.AssertionFailedError: null 
at junit.framework.Assert.fail(Assert.java:47) 
at junit.framework.Assert.assertTrue(Assert.java:20) 
at junit.framework.Assert.assertFalse(Assert.java:34) 
at junit.framework.Assert.assertFalse(Assert.java:41) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) 
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233) 
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1323) 
at groovy.lang.ExpandoMetaClass.invokeStaticMethod(ExpandoMetaClass.java:1082) 
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:62) 
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:48) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:165) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:173) 
at animal.AnimalTests.testMaximumCategories(AnimalTests.groovy:23) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 

...

我在做什么错?

回答

0

当我复制/粘贴你的代码时,它失败了,因为Animal类中没有title属性。这可能是将实际代码转换为演示代码的工件,但它可能会导致一些编译器怪异。尝试运行grails clean以强制进行完整编译。一旦我添加了title字段,它工作得很好。

+0

嘿,谢谢你的提示。这不是标题,而是我忘记设置的类别实例中的另一个属性:) – Moritz