No such property exception

1,687 views
Skip to first unread message

Mary

unread,
Jun 8, 2012, 12:12:45 PM6/8/12
to Spock Framework - User
I am trying to use a Mock with an Java Interface. I am receiving a
"No such property: mock1Listener for class ..."

Here is a portion of my Spec:
def "test listeners"() {
when:
def edition = new Edition(1,"Edition1", "Description", new
Date().getTime())
def mock1Listener = Mock(EditionNotifyListener)
def mock2Listener = Mock(EditionNotifyListener)

EditionNotify.getInstance().addEditionNotifyListener(mock1Listener)

EditionNotify.getInstance().addEditionNotifyListener(mock2Listener)
EditionNotify.getInstance().fireOnEditionInsert(edition)

then:
mock1Listener != mock2Listener // I added this
to make sure mock1Listener was still set.
1 * mock1Listener.onEditionInsert(edition) //I receive an
error on this line.
1 * mock2Listener.onEditionInsert(edition)
0 * mock1Listener.onEditionUpdate(edition)
0 * mock2Listener.onEditionUpdate(edition)
}

I receive "groovy.lang.MissingPropertyException: No such property:
mock1Listener for class:"

EditionNotifyListener is this interface:

public interface EditionNotifyListener {

public void onEditionInsert(final Edition edition);
public void onEditionUpdate(final Edition edition);
}

All my imports should be fine because everything else works.

Thanks for any help,

Mary

Peter Niederwieser

unread,
Jun 8, 2012, 5:59:13 PM6/8/12
to Spock Framework - User
The error message isn't very helpful, but mocks have to be declared
before the when-block. The when-block is supposed to exercise the code
under test; in your case, I'd probably move everything but the
`fireOnEditionInsert` to before the when-block.

Cheers,
Peter

MJ Beebe

unread,
Jun 8, 2012, 6:31:52 PM6/8/12
to spockfr...@googlegroups.com
Thanks Peter,

That was the answer!!
 
So there should be no def''s in the when clause.  I will keep reading.  I am thoroughly enjoying working with the Spock Unit tests. It keeps amazing me.  I am just having trouble interpreting the some of the error messages. 

Thank you for your time,

Mary

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To post to this group, send email to spockfr...@googlegroups.com.
To unsubscribe from this group, send email to spockframewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spockframework?hl=en.


Peter Niederwieser

unread,
Jun 9, 2012, 2:32:02 AM6/9/12
to spockfr...@googlegroups.com
You can have defs in when-blocks. But if you put an interaction (e.g. `1 * mock1Listener.onEditionInsert(edition)`) in a then-block, the corresponding mock has to be declared before the when-block. This is because the interaction is literally moved before the then-block so that Spock knows about it before exercising the code under test.

Cheers,
Peter
Reply all
Reply to author
Forward
0 new messages