Tuesday, May 24, 2016

JUNIT - Method Contract Testing

What is method contract testing:


Form the developer stand point it is very important to write Junit. I my career I had seen many different method/process/ways people write junit. Some write serious test cases, some writes for just sake of writing the test case and some test cases even puzzled me what exactly test case want to accomplish. 

Now as everyone is moving towards continuous integration (CI), automated builds, self-testing and rapid development writing good junit become the most critical part of development cycle.

In any development the most important aspect to test is Object, Method and its behavior with respect to change in its/dependent object state and evaluation path along with non functional aspects.

Any good JUNIT should revolve along the first two, change in state and evaluation path where as non functional aspects should be covered more precisely in integration testing.

While doing JUNIT developer needs to concentrate to method under test and all input / dependencies needs to be mocked. Test exactly how method will react to input, change in object state. Doing so if each and every method has good test case then eventually you will end up in writing good test case which can cover almost all scenarios ( That depends upon how good test case is written).

Coming to the point each method under test has certain method contracts. Following are different types of method contract:

1) Input Contract.
2) Output Contract.
3) Class Contract.
3) Exception Contract.
4) Behavioral Contract.
5) Non functional Contract.

Input Contract:

1) Null check - Method may or may not check null validity for input parameters.
In this case either current method or calling method should have null check unit test case.

2) Input Modification - Method may or may not modified the input objects.
In this case proper assert needs to be there for input object passed to check for modifications.


Output Contract:

1) return value contract - Based on the signature method can return new object or existing one.
Here proper asserts needs to be there to check for new object or existing object modifications.

2) return by reference - Method can return by modifying the input object which is passed by reference.
Proper assert needs to be there to unit test the same.

3) void contract - Method doesn't return any value.
Assert all the input / class object for any modification.


Class Contract:

1) Class contract - Method can change / initialize the accessible class /static variables.
Provide proper assert to track this changes.

Exception Contract:

1) forwarded exception - Method forward the exception instead of catching.
Mock the exception and assert the same.

2) new exception - Method creates and throws new exception.
Assert the new exception whether it is thrown or not.

Behavioral Contract:

Method can have one or many behavioral contract.
For all behavioral contract proper unit test and assert needs to be there.
Example:
If null is passed as input I will return null object which caller needs to handle.
If dependent call throw exception I will not rollback the changed input /class object.

Non functional Contract:

Method can have various non functional aspect like logging , transaction, concurrency etc.

All non functional aspect should be cover as part of integration testing and it should not be included as part of unit testing.

No comments:

Post a Comment