Hi Kevin,
Yeah, I've been using Mockito all year at work, though I have to say that
my preference is still for JMock.
The main thing that I don't like about Mockito is that when the mock needs
to be programmed with an expectation, then it's necessary to write both a
when... then... and a verify(...), which are both required but have subtly
different syntaxes. JMock in contrast requires only a single expectation to
be written.
Another thing I don't really like is writing an expectation for a method
that returns a void; the syntax is completely different because you can't
chain void methods.
And another thing that drives me mad is that it's either when...thenReturn
or it is stub...*to*Return. Really annoying, that difference.
On the other hand, I do like the @RunWith(MockitoJUnitRunner.class) and the
@Mock annotation. So the test you gave could be simplified to:
@RunWith(MockitoJUnitRunner.class)
public class SomeTest {
@Mock
DomainObjectContainer container;
@Mock
Authentication authentication;
@Before
public void setup() {
repository = new MemberRepositoryDefault();
repository.setContainer(container);
...
}
}
~~~
Anyway, if you prefer to use Mockito in the Isis tests rather than JMock, I
have no problems with that. But my tests are likely to carry on being in
JMock.
Cheers
Dan
On 10 December 2011 14:02, Kevin Meyer - KMZ <[email protected]> wrote:
>
> I discovered Mockito[1] in some other code that I was using, and it
> seems to be easier to use than JMock:
>
> Set-up (and it can mock classes, too, not just interfaces):
> container = mock(DomainObjectContainer.class);
>
> Governing behaviour:
>
>
> when(container.newTransientInstance(Promotion.class)).thenReturn(containedPromotion);
>
>
> when(container.newTransientInstance(Member.class)).thenReturn(containedMember);
>
> Checking behaviour:
> verify(container).persistIfNotAlready(member);
>
> Other interactions can be detected with:
> verifyNoMoreInteractions(container);
>
>
> From the javadoc:
> verify(mock).someMethod("some arg");
> Above is equivalent to:
> verify(mock, times(1)).someMethod("some arg");
>
> An entire test looks like:
>
> @Before
> public void setup() {
> container = mock(DomainObjectContainer.class);
> authentication = mock(Authentication.class);
>
> repository = new MemberRepositoryDefault();
> repository.setContainer(container);
> repository.setAuthentication(authentication);
>
> containedMember = new Member();
> containedMember.setAddress("contained");
> containedMember.setContainer(container);
> containedMember.setAuthentication(authentication);
> containedMember.setMembers(repository);
>
>
>
> when(container.newTransientInstance(Member.class)).thenReturn(containedMember);
> }
>
>
> @SuppressWarnings("unchecked")
> @Test
> public void ensureThatNewMemberHasApplicantStatus() {
>
> when(authentication.memberExists(any(String.class))).thenReturn(false);
> Member member = repository.newMember("Another Test", "Member",
> "email@domain");
> assertNotNull(member);
> verify(container).persistIfNotAlready(member);
> verify(authentication).memberExists("atmember");
> verify(container).newTransientInstance(Member.class);
> verify(container,
> atLeastOnce()).newPersistentInstance((Class<AbstractDomainObject>)
> anyObject());
> verifyNoMoreInteractions(container);
> assertThat(member.getMemberLevel(),
> is(equalTo(MemberLevel.MEMBER_APPLICANT)));
> assertThat(member.getState(), is(equalTo(State.NEW)));
>
> ongoingMember = member;
> }
>
>
>
> I found this easier to use than setting up a Mockery context..
>
>
> Anyone have any other experiences?
>
> Add to pom.xml:
> <dependency>
> <groupId>org.mockito</groupId>
> <artifactId>mockito-all</artifactId>
> <version>1.9.0-rc1</version>
> <scope>test</scope>
> </dependency>
>
>
> [1] http://code.google.com/p/mockito/
>
> http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html
>
>
>