gpt4 book ai didi

unit-testing - Grails 单元测试中的 "junit.framework.AssertionFailedError: null"

转载 作者:行者123 更新时间:2023-11-28 20:57:42 25 4
gpt4 key购买 nike

我是 grails 的新手,正在尝试运行一些测试。我有两个类(class):

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)

...

我做错了什么?

最佳答案

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

关于unit-testing - Grails 单元测试中的 "junit.framework.AssertionFailedError: null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8195433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com