Thursday, 11 March 2010

Mockito

Mockito is a framework to help you create stubs and mocks quickly if you do Unit Testing.

Check the link out and the tutorial. 

http://bitbucket.org/loomis/mockito-flex/wiki/Home

Tutorial for version 1.0

This tutorial is unit testing framework agnostic. For examples on how to use it with specific unit testing frameworks please check the integrations section.

Let's

Before you will be able to create your mock objects you need to tell Mockito to prepare for it. You do it by calling:

1 
mockito
.prepareClasses([...]) ;

and providing all the classes to mock in given test case. Since the bytecode generation is an asynchronous process it is recommended to use one of the integration test cases as they address this issue. You may find more answers inAsmock documentation.

After preparing classes you can create mock objects by invoking:

1 
var dependency:Dependency = Dependency(mockito.mock(Dependency)); 

Then setup the System Under Test (SUT)

1 
var sut:Sut = new Sut(dependency); 

And execute tested code.

1 
sut.testedFunction(10); 

Given the testedFunction looks like this:

1 2 3 4 
function testedFunction(input:int):void  {  dependencyResult = dependency.someOperation(input);  } 

Notice that there is no 'unexpected call' exception.

Instead you are free to choose what you want to verify:

1 
verify().that(dependency.someOperation(input)); 

The full test would look like this:

...  mockito.prepareClasses([Dependency]);  ...  var dependency:Dependency = Dependency(mockito.mock(Dependency));  // given  var sut:Sut = new Sut(dependency);  // when  sut.testedFunction(10);  // then  mockito.verify().that(dependency.someOperation(10)); 

As you can see, verification happens where assertions go. Not before the tested code. Important note is that verify() is equivalent to verify(times(1)).

If you need to stub dependency, you define it upfront.

1 
mockito.given(dependency.someOperation(10)).willReturn(1); 

When used in test it would look like this:

...  mockito.prepareClasses([Dependency]);  ...  var dependency:Dependency = Dependency(mockito.mock(Dependency));  // given  var sut:Sut = new Sut(dependency);  mockito.given(dependency.someOperation(10)).willReturn(1);  // when  sut.testedFunction(10);  // then  assertEquals(1, sut.dependencyResult); 

It may be useful to verify or define stubbing with various arguments at a time or have ability of matching the specific cases. For that purpose Mockito provides Matchers. Below example verifies any arguments passed to the function:

1 
mockito.verify().that(dependency.someOperation(mockito.any())); 

Similar you can do with stubbing:

1 
mockito.given(dependency.someOperation(any())).willReturn(1); 

As you can see you can either use explicit values or matchers when defining stubs or verifying. But you cannot mix it within single stub definition or verification. So for instance:

1 
mockito.verify().that(progress.update(10, mockito.any())) 

is invalid. Mockito will not be able to figure out which argument type to match against the any() matcher. You may want to verify multiple executions of a method at time. It's easy. Just tell how to verify:

1 
mockito.verify(mockito.times(3)).that(dependency.someOperation(mockito.any())); 

Sometimes you may want to make sure method has not been called. Do it by verifying:

Posted via email from fasanya's posterous

No comments: