Wiki

Clone wiki

mockito-flex / tutorials / Tutorial1.0

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:

    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 in Asmock documentation.

After preparing classes you can create mock objects by invoking:

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

Then setup the System Under Test (SUT)

     var sut:Sut = new Sut(dependency);

And execute tested code.

     sut.testedFunction(10);

Given the testedFunction looks like this:

     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:

     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.

     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:

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

Similar you can do with stubbing:

     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:

     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:

     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:

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

If you miss a verifier you can write it on your own by implementing Verifier interface.

If you need to perform a matching that is not provided by the Mockito, you can write your custom Matcher. You start off with implementing the Matcher interface:

     public class HashOnlyCapitalLettersMatcher implements Matcher
     {
          public function matches(value::Boolean
          {
              var string:String = value as String;
              var hasOnlyCapitalLetters:Boolean = ...;
              return hasOnlyCapitalLetters;
          }
     }

Then you need to record a matcher use by calling argThat() function that puts aside a matcher for later use:

     mockito.verify().that(system.login(mockito.argThat(new HashOnlyCapitalLettersMatcher())));

A good practice is to create a matcher recording function somewhere and name it after the matcher. It's important to return a wildcard from the function to let it work with any arugment of the function

     function hasOnlyCapitalLetters():     {
         mockito.argThat(new HashOnlyCapitalLettersMatcher());
     }

Don't worry, you don't have to put mockito everywhere. There is an integration test case for flexunit that gives you all the needed method out of the box.

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.