Skip to content

ts mockito 1.x to 2.x migration guide

Maciej Kuster edited this page Apr 28, 2017 · 16 revisions

Breaking changes in 2.x version

Reason (why?)

The most important thing, why 2.x version breaks current API is to be more compatible with Java Mockito version of recording multiple behaviors

Recording multiple behaviors

In 1.x version to return multiple values one by one we had to do:

when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(anyNumber()).thenReturn('two');
when(mockedFoo.getBar(anyNumber())).thenReturn('three');

In 2.x version we can do it like in Java:

when(mockedFoo.getBar(anyNumber())).thenReturn('one').thenReturn('two').thenReturn('three');

// Or even easier:
when(mockedFoo.getBar(anyNumber())).thenReturn('one', 'two', 'three');

Last stubbing is more important

Because in 1.x version recording multiple has been separated to multiple when calls it was setting return values order. For example:

when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(anyNumber()).thenReturn('two');
when(mockedFoo.getBar(anyNumber())).thenReturn('three');

const foo = instance(mockedFoo);

foo.getBar(1); // one
foo.getBar(1); // two
foo.getBar(1); // three
foo.getBar(1); // null - !!! no more defined values

In 2.x version last stubbing is more important and will be repeated infinitely:

when(mockedFoo.getBar(anyNumber())).thenReturn('one');
when(mockedFoo.getBar(anyNumber()).thenReturn('two');
when(mockedFoo.getBar(anyNumber())).thenReturn('three');

const foo = instance(mockedFoo);

foo.getBar(1); // three - last stubbing overrides previous ones!
foo.getBar(1); // three
foo.getBar(1); // three

But of course you can define multiple return values:

when(mockedFoo.getBar(anyNumber())).thenReturn('one', 'two', 'three');

const foo = instance(mockedFoo);

foo.getBar(1); // one
foo.getBar(1); // two
foo.getBar(1); // three

Summary

As you can see, the only thing you got to change is to replace all multiline calls of stubbing into one, using fluent API. And if you want to get null after your values you got to define it as last one behavior.

Clone this wiki locally