[英]“junit.framework.AssertionFailedError: null” in Grails Unit Test
I'm new to grails and am trying to get some tests running. 我是grails的新手,正在尝试运行一些测试。 I have two classes: 我有两节课:
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
}
}
Now I wrote a test that should check that adding more than two categories fails and that the third category would not be part of the categories: 现在,我编写了一个测试,该测试应检查添加两个以上类别是否失败,并且第三个类别将不属于这些类别:
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))
}
}
This test always fails in the last line and the stacktrace is the following 该测试总是在最后一行失败,并且堆栈跟踪如下
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)
... ...
What am I doing wrong? 我究竟做错了什么?
When I copy/pasted your code it failed since there's no title
property in the Animal
class. 当我复制/粘贴您的代码时,它失败了,因为Animal
类中没有title
属性。 This might be an artifact of converting real code to demo code, but it could cause some compiler weirdness. 这可能是将实际代码转换为演示代码的产物,但是可能会导致一些编译器异常。 Try running grails clean
to force a full compile. 尝试grails clean
地运行grails clean
以强制完全编译。 Once I added a title
field it worked fine. 一旦添加title
字段,它就可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.