简体   繁体   English

Grails单元测试中的“ junit.framework.AssertionFailedError:空”

[英]“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.

相关问题 Robotium测试失败,并显示以下内容:junit.framework.AssertionFailedError:无法单击视图! - Robotium test fails with: junit.framework.AssertionFailedError: View can not be clicked! 获取junit.framework.AssertionFailedError:使用Unit和Mockito时,[package]中没有找到任何测试 - Getting junit.framework.AssertionFailedError: No tests found in [package] when using Unit and Mockito junit.framework.AssertionFailedError并且由于空白而没有可运行的方法 - junit.framework.AssertionFailedError and no runnable methods because of whitespace junit.framework.AssertionFailedError:构造函数中的异常:(java.lang.NoClassDefFoundError) - junit.framework.AssertionFailedError: Exception in constructor: (java.lang.NoClassDefFoundError) 浓咖啡。 引起:junit.framework.AssertionFailedError: 'with error: is “...” 与所选视图不匹配 - Espresso. Caused by: junit.framework.AssertionFailedError: 'with error: is “…” doesn't match the selected view Grails单元测试无效性 - Grails Unit Test Null Strangeness Grails单元测试导致空实例 - Grails unit test resulting in null instance Grails Interceptor模型在单元测试中为空 - Grails Interceptor model is null in unit test Grails单元测试不起作用-NoClassDefFoundError:junit / framework / TestCase - Grails unit testing not working - NoClassDefFoundError: junit/framework/TestCase 如何在单元测试用例(grails,junit)中断言“render”中的值 - how to assert values inside “render” in unit test case (grails ,junit)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM