6. Behaviour Verification

Behaviour verification is about ensuring that code run by the method-under-test behaves in a certain way, typically by checking that it performs expected invocations using the expected parameters.  This differs from state verification which is about ensuring that after the method-under-test is run, the returned value and/or the state of the object-under-test or any other potentially affected objects are as expected.  With Exemplars, State verification is achieved using the expects property.  You can also perform behaviour verification, using the verify property.  This section covers how this is achieved.

Traditionally mock objects have been used to facilitate behaviour verification.  These are created using a mock object library which generates a mock implementation of a given interface.  Within your test code, you determine which invocations (or “interactions”) are expected on the mock object prior to invoking the method-under-test, and post-invocation, you call a method to verify that the expected interactions took place.

Sureassert uses an alternative approach – it does not tie behaviour verification to mock objects.  Because Sureassert instruments all your code internally, it is capable of verifying the behaviour of any of your project classes.  In other words, you are not constrained to verifying behaviour on mock or stub instances.

Sureassert draws lines between state verification, stubs and behaviour verification and lets you easily and logically combine them to cater for most common use-cases.   Common use-cases can handled with Exemplar properties as follows:

Use Case Exemplar properties to define
State verification (check return value and/or object state) expect
Stub methods (pre-program functionality and return value of a method) stub
Stub methods and verify that stubs are executed vstub
Stub methods and perform state verification stub/vstub + expect
Behaviour verification (expect a given invocation to take place) verify
Verify interactions with a stubbed method (aka mocking) stub/vstub + verify
Verify interactions with a stubbed method and perform state verification stub/vstub + verify + expect

Often, teams either prefer state verification with stubs over behaviour verification (traditionally with mocks) or vice-versa, and it is generally best-practice to adopt and stick to a particular style for your project rather than chopping and changing.  Sureassert gives you the flexibility to adopt whichever testing style fits your project best.

Previous Page   |   Next Page   |   Table of Contents

Comments are closed.