Johan,
Thanks. I was trying to use just PowerMock.when(). And when I am
testing an interface class, I cannot create the spy object. And the
PowerMock.when() won't take a mock object.
Please look at the testGetAllServiceProducers().
// PowerMockito.when have problem taking the mockito mock object -
only take spy real object
// PowerMockito.doReturn(serviceArtifacts).when(providerDaoMock,
"getAllServiceProducers");
Mockito.when(providerDaoMock.getAllServiceProducers()).thenReturn(serviceArtifacts);
Set<ServiceProducer> serviceProducers = (Set<ServiceProducer>)
invokeMethod(providerserviceImpl, "getAllServiceProducers");
assertEquals(1, serviceProducers.size());
assertTrue(serviceProducers.contains(new ServiceProducer(expectedId,
expectedName)));
}
@Test
@SuppressWarnings("unchecked")
public void getAllServiceProducers_empty() throws Exception {
Mockito.when(providerDaoMock.getAllServiceProducers()).thenReturn(new
HashSet<ServiceArtifact>());
Set<ServiceProducer> actual = (Set<ServiceProducer>)
invokeMethod(providerserviceImpl, "getAllServiceProducers");
assertTrue(actual.isEmpty());
}
}
I wrote SampleServiceImplMockitoTest, HelloWorldMockitoTest,
ProviderServiceImplMockitoTest, ServiceRegistratorMockitoTest in
mockito style. You can update that anyway you want and put that in
your repository. I can send you in email, did not see the attachment
option here.
thanks
mantat
On Jan 11, 11:08 pm, Johan Haleby <
johan.hal...@gmail.com> wrote:
> Hi,
>
> You can use Mockito's when in combination with PowerMock (however I don't
> think you should explicitly write Mockito.when(..) or PowerMockito.when(..),
> use static imports instead). The difference between the two is that when
> using "when" you call the original method if you're using "spy" whereas usng
> doReturn never calls the real method. So for example if you need partial
> mocking and cannot allow the method to be called then you should use
> doReturn.
>
> Do you have any problem with using the "when" method in PowerMockito? If I
> remember it correctly it should simply delegate to Mockito.when so there
> should be no difference.
>
> It would also be really cool if you could share the tutorial with us when
> you're done.
>
> Regards,
> /Johan
>
> >
powermock+...@googlegroups.com<
powermock%2Bunsu...@googlegroups.com>
> > .