How to verify if an object has certain property?

前端 未结 4 1999
轻奢々
轻奢々 2020-12-29 01:46

I want to use either a value of expected property or a specified default. How to achieve this in groovy?

Let\'s look at the example:

def printName(ob         


        
4条回答
  •  -上瘾入骨i
    2020-12-29 02:13

    If I simply want to assert that an object has some property, I just test the following:

    assertNotNull(myObject.hasProperty('myProperty').name)
    

    If myObject does not have myProperty the assertion will fail with a null pointer exception:

    java.lang.NullPointerException: Cannot get property 'name' on null object
    

提交回复
热议问题